Reading JSON file with Python 3

Question:

I’m using Python 3.5.2 on Windows 10 x64. The JSON file I’m reading is this which is a JSON array containing 2 more arrays.

I’m trying to parse this JSON file using the json module. As described in the docs the JSON file must be compliant to RFC 7159. I checked my file here and it tells me it’s perfectly fine with the RFC 7159 format, but when trying to read it using this simple python code:

with open(absolute_json_file_path, encoding='utf-8-sig') as json_file:
    text = json_file.read()
    json_data = json.load(json_file)
    print(json_data)

I’m getting this exception:

Traceback (most recent call last):
  File "C:Program Files (x86)JetBrainsPyCharm 4.0.5helperspydevpydevd.py", line 2217, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "C:Program Files (x86)JetBrainsPyCharm 4.0.5helperspydevpydevd.py", line 1643, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:Program Files (x86)JetBrainsPyCharm 4.0.5helperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"n", file, 'exec'), glob, loc) 
  File "C:/Users/Andres Torti/Git-Repos/MCF/Sur3D.App/shapes-json-checker.py", line 14, in <module>
    json_data = json.load(json_file)
  File "C:UsersAndres TortiAppDataLocalProgramsPythonPython35-32libjson__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:UsersAndres TortiAppDataLocalProgramsPythonPython35-32libjson__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:UsersAndres TortiAppDataLocalProgramsPythonPython35-32libjsondecoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersAndres TortiAppDataLocalProgramsPythonPython35-32libjsondecoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I can read this exact file perfectly fine on Javascript but I can’t get Python to parse it. Is there anything wrong with my file or is any problem with the Python parser?

Asked By: Andres

||

Answers:

Try this

import json

with open('filename.txt', 'r') as f:
    array = json.load(f)

print (array)
Answered By: lcastillov

Based on reading over the documentation again, it appears you need to either change the third line to

json_data = json.loads(text)

or remove the line

text = json_file.read()

since read() causes the file’s index to reach the end of the file. (I suppose, alternatively, you can reset the index of the file).

Answered By: Will Molter

For python3, only the below worked for me – none of the above.

import json

with open('/emp.json', 'r') as f:

     data=f.read()

print(data)
Answered By: Gautam De
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.