2

I have a question concerning an issue I ran into while using the json lib in Python.

I'm tying to read a json file using the json.load(file) command using the following code:

import json

filename= '../Data/exampleFile.json'
histFile= open(filename, 'w+')
print(json.load(histFile))

The JSON file I am trying to read is valid according to some website I found: a screenshot of that validation, because I'm new and still lack the reputation...

The error message I'm getting is the following:

File ".\testLoad.py", line 5, in <module>
print(json.load(histFile))
File "C:\Users\...\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\...\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\...\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\...\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Alright, so I believe it is not the file that is the issue, but the json.load(file) works for me in other cases.

Sadly I was not able to figure this error-message out on my own, so it would be amazing if someone with some more experience dealing with Python-JSON interaction could maybe help me out.

4
  • ...post the JSON? Commented Sep 7, 2018 at 21:40
  • My psychic powers are telling me that your file has a UTF-8 BOM and you need to open it with encoding='utf-8-sig'. Commented Sep 7, 2018 at 21:42
  • @jwodder: nope. Commented Sep 7, 2018 at 21:43
  • 1
    @jwodder I'm pretty sure json.load (and even loads) handles UTF-8-sig; ever since… whichever version added binary files (and bytes) in UTF-8 or UTF-16, it's also handled UTF-8-sig, even (although not documented) handling UTF-8-sig files opened as UTF-8 text. Commented Sep 7, 2018 at 22:00

1 Answer 1

8

You opened the file for writing:

histFile= open(filename, 'w+')
#                        ^^^^

The w mode first truncates the file, so the file is empty (it doesn't matter here that the file can also be read from, the + sees to that but the file is truncated nonetheless). See the open() function documentation:

'w': open for writing, truncating the file first)

There is no JSON data in it to parse. This is why the exception tells you that parsing failed at the very start of the file:

Expecting value: line 1 column 1 (char 0)

There is no data in line one, column one.

If you wanted to open a file for both reading and writing without truncating it first, use 'r+' as the file mode.

Sign up to request clarification or add additional context in comments.

4 Comments

Would 'a' as an open mode be equivalent to 'r+' in this case?
@domochevski: no, some OSes will not let you seek to anywhere in the file before the current file size (so you can't alter the portion that was there when you opened the file).
Thank for the precision. I did not think about the differences that might exist between OSes.
Thank you very much! This solved the issue. I used w+ because the file I want to open may have to be created first, but I'm gonna deal with that using a try statement now I guess. I did read this description of w+, but must've completely read over the part telling me about the truncation. ` w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. `

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.