0
votes

I have a code:

dguid='2016A0011M4B'
income='7'
ethnic='3'
language='10'
url = 'https://www12.statcan.gc.ca/rest/census-recensement/CPR2016.json?lang=E&dguid={}&topic={}&notes=1'.format(
    dguid,
    ethnic)

!wget -q -O 'data.json' url
with open('data.json') as json_data:
data = json.load(json_data)

It gives me an Error enter image description here

--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) in () 9 get_ipython().system("wget -q -O 'data.json' url") 10 with open('data.json') as json_data: ---> 11 data = json.load(json_data)

/opt/conda/envs/DSX-Python35/lib/python3.5/json/init.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 266 cls=cls, object_hook=object_hook, 267 parse_float=parse_float, parse_int=parse_int, --> 268 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) 269 270

/opt/conda/envs/DSX-Python35/lib/python3.5/json/init.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 317 parse_int is None and parse_float is None and 318 parse_constant is None and object_pairs_hook is None and not kw): --> 319 return _default_decoder.decode(s) 320 if cls is None: 321 cls = JSONDecoder

/opt/conda/envs/DSX-Python35/lib/python3.5/json/decoder.py in decode(self, s, _w) 337 338 """ --> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 340 end = _w(s, end).end() 341 if end != len(s):

/opt/conda/envs/DSX-Python35/lib/python3.5/json/decoder.py in raw_decode(self, s, idx) 355 obj, end = self.scan_once(s, idx) 356 except StopIteration as err: --> 357 raise JSONDecodeError("Expecting value", s, err.value) from None 358 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

what is wrong?

1
If I use loads instead of load method it will give me Error: the JSON object must be str, not 'TextIOWrapper'Ramziya Greedy
!wget -q -O 'data.json' url does not look like valid Python code...Ralf
There are some data science notebook interactive interfaces such as Jupyter/Ipython that support running shell commands like that (in the middle of python code, for example). Like this: twitter.com/python_tip/status/929984742421942272/photo/1Alexandre Juma

1 Answers

0
votes

The JSON returned by that URL is not valid. This is clearly identified by the exception.

The JSON payload has a prefix of "//" that renders the JSON invalid:

//{"COLUMNS":["PROV_TERR_ID","PROV_TERR_NAME_NOM",...

You must strip these first 2 characters. You can do it like this:

with open('data.json') as json_data:
    line = json_data.readlines()
    line[0] = line[0][2:]
    data = json.loads(line[0])
    print(data)

Tested correctly om python 3.6.1