As im new to coding and making my own small projects i finally encountered a problem that has bugged me for the last couple of days.
So basically what i want to do is to append my dict to a list and then dump it to a json file.
username = request.form["username"]
password = request.form["password"]
checkbox2 = request.form["checkbox2"]
userDict = {
"username" : username,
"password" : password,
"checkbox2" : checkbox2,
}
print(userDict)
if os.path.exist("data.json"):
with open("data.json", "r") as f:
all_data = json.load(f)
else:
all_data = []
all_data.append(userDict)
with open("data.json", "w") as f:
json.dump(all_data, f)
This is the code i'm running with at the moment. The Traceback i get is when sending my forms.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Marco\Desktop\test\server.py", line 28, in sendinfo
all_data = json.load(f)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-
32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-
32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-
32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Marco\AppData\Local\Programs\Python\Python36-
32\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)
My expected json output if data.json is empty is following
{
"all_data": [{
"username": "test",
"password": "test2",
"checkbox2": "on"
}]
}
If the file do have "all_data": in the file already then my expected output should be
{
"all_data": [{
"username": "test",
"password": "test2",
"checkbox2": "on"
},
{
"username": "test3",
"password": "test4",
"checkbox2": "on"
}
]
}
This issue have been bugging me for 3 days, and i thought this would be my last resort because i'm the type of person who would like to solve it myself.
Thanks in advance.
Does your data.json
already exist but is empty? The JSON library will throw an error in such a case! You should fill a string like []
in your data.json to avoid this error.