. Advertisement .
..3..
. Advertisement .
..4..
I have the following python code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved? Here is the code that I am running:
with open(filename, "r") as f:
data = json.loads(f.read())
with open(filename, "r") as f:
data = json.load(f)
And this is the error text I receive:
Traceback (most recent call last):
File "myfile.py", line 8, in myfunction
config = json.loads(f.read())
File "c:\python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "c:\python27\lib\json\decoder.py", line 360, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\python27\lib\json\decoder.py", line 378, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
The cause: In many circumstances when the built-in
json
module is unclear, I’ve found that thesimplejson
module produces more descriptive errors. For example, there’s a comma after the last entry in a list:Also with
simplejson
:Solution: After I use
sudo pip install simplejson
to installsimplejson
.And then, the error is figured out.
It won’t be possible to get Python to correct the JSON. To find a linter online, you will need one .
This will indicate any errors in the JSON that you are trying decode.