G
G
GroMan_L2022-03-24 21:56:04
Python
GroMan_L, 2022-03-24 21:56:04

json.decoder.JSONDecodeError error when reading json file, how to fix?

Hello, when I try to read a json file, I get an error:
Traceback (most recent call last):
File "C:\Users\GroMan\PycharmProjects\PaperRockScissors\app.py", line 125, in
main()
File "C:\ Users\GroMan\PycharmProjects\PaperRockScissors\app.py", line 64, in main
dire = json.load(jsonconfig)
File "C:\Users\GrroMan\AppData\Local\Programs\Python\Python39\lib\json\__init__ .py", line 293, in load
return loads(fp.read(),
File "C:\Users\Mary\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\GroMan\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337,in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Mary\AppData\Local\Programs\Python\Python39\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)

Process finished with exit code 1

Here is the code to read the file:

jsonconfig = open('media/config.json', 'r', encoding='utf-8')
 if json.load(jsonconfig)['DEBUG']:
     dire = json.load(jsonconfig)
     dire = dire['directory']


Here is the text in the json file:
{"DEBUG": true, "directory": "C:/Games/MudRunner/Media"}
.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-03-24
@GroMan_L

if json.load(jsonconfig)['DEBUG']:
     dire = json.load(jsonconfig)

Trying to upload the file twice. Apparently it meant something like this:
data = json.load(jsonconfig)
if data['DEBUG']:
     dire = data['directory']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question