0

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"}]
5
  • You need to provide the jso or a segment of it, including the start and the end so we can see the shape of it. Commented Dec 30, 2018 at 22:06
  • Could be a BOM in the file. That happens when editing it with some text editors and python doesn't like it. Commented Dec 30, 2018 at 22:10
  • Ok, @ShailynOrtiz I added it, my bad. Commented Dec 30, 2018 at 22:11
  • try this: stackoverflow.com/questions/13156395/… Commented Dec 30, 2018 at 22:11
  • @GarrGodfrey AttributeError: 'str' object has no attribute 'decode' Commented Dec 30, 2018 at 22:16

2 Answers 2

2

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.

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

2 Comments

The error itself was weird, however I managed to get a solution. I'm gonna post it.
And yes, the JSON was empty due to an error in the save function.
0
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!

Comments

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.