1

I need to get data from a json page and convert it to a list.

import json
import requests

j = requests.get('http://www.example.com/Portals/0/StaticData/data.js')
abc = json.loads(j.content)

However, error occurred

ValueError: No JSON object could be decoded

what I need is

mylist = ['AALI','ABBA','ABDA'.......]
mylist1 = ['Astra Agro Lestari Tbk','Mahaka Media Tbk','Asuransi Bina Dana Arta Tbk',.......]
bkcollection
  • 913
  • 1
  • 12
  • 35
  • That endpoint (that `idx.co.id...` URL) doesn't return JSON, but javascript code with a variable assignment `var symbolsCode = [{"` ... You might wanna look a this thread?: http://stackoverflow.com/questions/390992/javascript-parser-in-python Maybe it helps – Savir Mar 06 '17 at 03:42

1 Answers1

2

You don't have a valid JSON returned, you can clean it up before reading it in with json module:

js = j.content.decode("utf-8").split("=")[-1].strip().strip(';')

json.loads(js)

#[{'code': 'AALI', 'name': 'Astra Agro Lestari Tbk'},
# {'code': 'ABBA', 'name': 'Mahaka Media Tbk'},
# {'code': 'ABDA', 'name': 'Asuransi Bina Dana Arta Tbk'},
# {'code': 'ABMM', 'name': 'ABM Investama Tbk'},
# {'code': 'ACES', 'name': 'Ace Hardware Indonesia Tbk'},
# ...

To unpack the result into two lists:

mylist, mylist1 = zip(*((d['code'], d['name']) for d in json.loads(js)))
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • It works, but still have 'u' in the mylist and mylist1. Also, can you add explanation for `js = j.content.decode("utf-8").split("=")[-1].strip().strip(';')` and `json.loads(js)`? – bkcollection Mar 06 '17 at 05:56