Daniel
Daniel

Reputation: 375

Python JSON Loading file errors

I'm trying to load a file to a variable, but I get the error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) in whitelist = json.load(f), what am I doing wrong?

def load_whitelist():
    global whitelist
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='r') as f:
            whitelist = json.load(f)
            f.close()
            print(whitelist)

def save_whitelist():
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='w') as f:
            json.dump(whitelist, f, sort_keys=False)
            f.close()

Full Traceback:

Traceback (most recent call last):
  File "PC200.py", line 970, in <module>
    load_whitelist()
  File "PC200.py", line 51, in load_whitelist
    whitelist = json.load(f)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\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)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020022935828>

The JSON

[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]

Upvotes: 0

Views: 1734

Answers (2)

Daniel
Daniel

Reputation: 375

def load_whitelist():
    global whitelist
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='r') as f:
            whitelist = json.load(f)
            f.close()

def save_whitelist():
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    print(whitelist)
    with open(wl, mode='w') as f:
        json.dump(whitelist, f, sort_keys=False)
        f.close()

It seems like there was an error with saving the whitelist and it deleted the JSON contents, the code itself was ok (I removed the if os.path.isfile(wl): as it was unnecessary). Thank you all for trying to help!

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148890

Not really an answer but it cannot fit into a comment. With only the error message, it is impossible to know what happens. This error can be reproduced with an empty file, or with an incorrect encoding. Example simulating an empty file:

>>> import json
>>> import io
>>> fd = io.StringIO()
>>> wl = json.load(fd)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    wl = json.load(fd)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Program Files (x86)\Python37-32\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)

Example simulating an UTF16 encoded file read as Latin1:

>>> t = '''[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]'''
>>> tt = t.encode('utf16').decode('latin1')
>>> fd=io.StringIO(tt)
>>> wl = json.load(fd)
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    wl = json.load(fd)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Program Files (x86)\Python37-32\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)

The only way to discriminate the problem is to read and print the content of the file:

with open(wl, mode='r') as f:
    data = f.read()                      # read file conten
    print(data)                          # display it as text
    print([hex(ord(i)) for i in data])   # and as an hex dump

That way you will be sure of the way Python actually reads the file.

Upvotes: 2

Related Questions