39

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)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 2217, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 1643, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\_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:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.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?

7
  • Something is wrong with the encoding of the file. For what it's worth, requests.get("http://pastebin.com/raw/Yjs6FAfm").json() works :) Commented Jul 28, 2016 at 19:01
  • @cricket_007 it's possible as I had to use encoding='utf-8-sig' to avoid weird characters at the start of the file because the file is UTF-8-BOM not UTF-8 Commented Jul 28, 2016 at 19:05
  • Right, the problem is at the start of the file as evident by line 1 column 1 Commented Jul 28, 2016 at 19:06
  • @cricket_007 Yes, the answer from @Will Molter works, reading the text content instead of the file, maybe json#load is ignoring the file encoding? Commented Jul 28, 2016 at 19:08
  • Don't know about that, I thought it just did .read() on the file anyway. Commented Jul 28, 2016 at 19:09

3 Answers 3

66

Try this

import json

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

print (array)
Sign up to request clarification or add additional context in comments.

5 Comments

I still get the same exception, 'r' is the default opening mode
@Andres i guess the problem is with your local file. Check again.
What encoding did you use when saving the file, probably it is
@Andres I saved it as UTF-8
hey @Andres, sorry for digging on an old thread but how do you save the .json file in utf-8 on a windows machine? I don't see an option in Json Dumps function. There is a token.json I've created on the windows machine. and it gives an encoding error to utf8 for the start byte when it's deployed to GCP which of course uses ubuntu machines on the cloud. any suggestion is appreciated :)
20

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).

2 Comments

This works, but why can't I open the file directly? According to this docs.python.org/3/library/json.html#json.load I should be able to.
Ah, I figured it out; if you call read() first, then the the file object has its index at the end of the file and there is no longer anything left to make a json out of.
-1

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)

1 Comment

The problem with this answer is that the variable "data" is a string, not a structured object like a dict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.