skip to Main Content

I am developing a python script, and it must read a JSON file.

Here is the code i have:

import json
f = open('data.json', 'r')
a = json.load(f)

for some reason, I am encountering the following error:

File "C:Program FilesPython39libjson__init__.py", line 293, in load
     return loads(fp.read(),   File "C:Program FilesPython39libjson__init__.py", line 346, in loads
     return _default_decoder.decode(s)   File "C:Program FilesPython39libjsondecoder.py", line 337, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
   File "C:Program FilesPython39libjsondecoder.py", line 355, in raw_decode
     raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 22 column 1 (char 796)

I have tried to investigate this issue on the internet, but could not find anything that may be helpful.

2

Answers


  1. Always validate your JSON with jq.

    Install jq

    $ jq < file.json

    Perhaps it is some quoting or unicode issue.

    Then re run your python code.

    Login or Signup to reply.
  2. The file is in binary form, you can try reading in utf-8:

    with open('data.json', encoding='utf-8') as f:
        a = json.load(f)
    

    with handles the correct opening and closing of the file, the encoding argument to open ensures the file is read using the correct encoding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search