Sam Wood
Sam Wood

Reputation: 418

Why am I getting a JSONDecodeError when trying to load a JSON file in Python?

I'm trying to load a JSON file in to Python, but it's giving me an JSONDecodeError error which seems to suggest the file is empty... which I know is not true.

I've reviewed similar questions I can find on here (65466227 & 62286816), and while one or two provide useful alternatives... that's not really the point. I feel like what I'm doing should work but it doesn't.

I put my JSON in to jsonlint.com and it confirmed that my JSON is indeed valid.

Error message/traceback:

Traceback (most recent call last):
  File "utils/fmt_test.py", line 62, in <module>
    test_cases_json = json.load(jf)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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)

My Python code:

test_case_file, _ = os.path.splitext(fmt_file)
test_case_file = f"{test_case_file}.json"
if os.path.isfile(test_case_file):
    with open(test_case_file, "r") as jf:
        test_cases_json = json.load(jf)

My JSON:

{
    "tests": [
        {
            "id": "746fd83s23dy",
            "event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING TEST LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST2 LFA_DUMMYSTRING",
            "match": "True"
        },
        {
            "id": "990gb98s34dm",
            "event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
            "match": "True"
        },
        {
            "id": "100ma09x31ui",
            "event": "%t localhost LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST3 LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
            "match": "True"
        }
    ]
}

Any help is much appreciated, thanks.

Upvotes: 3

Views: 8094

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177674

There is likely a UTF-8 BOM encoded at the beginning of the file since it is complaining about the first byte. Open with encoding='utf-8-sig' and it will be removed if present:

>>> import json
>>> data = {}
>>> with open('test.json','w',encoding='utf-8-sig') as f:  # write data with BOM
...   json.dump(data,f)
...
>>> with open('test.json') as f:  # read it back
...  data2 = json.load(f)
...
Traceback (most recent call last):
  <traceback removed>
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)  # same error
>>> with open('test.json',encoding='utf-8-sig') as f:
...  data2 = json.load(f)
...
>>> # worked

Upvotes: 3

Related Questions