I'm working with python, I have a json structure into a dictionary and I have exported it into a file. Now I need to reload the structure from the file, I want to reload it into a dictionary (in order to update it) but I'm experiencing some problems. This is my code:
#export the structure
with open('data.json','w') as f:
data = {}
data['test'] = '1'
f.write(json.dumps(data))
#reload the structure
with open('data.json','r') as f:
dict = {}
dict = json.loads(f.read())
The error is: No JSON object could be decoded.
asked Mar 25, 2015 at 13:54
Chris Cris
2151 gold badge3 silver badges13 bronze badges
1 Answer 1
Try
with open('data.json', 'w') as f:
f.write(json.dumps(data))
with open('data.json', 'r') as f:
json.load(f)
answered Mar 25, 2015 at 13:59
cangoektas
7196 silver badges7 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Chris Cris
the problem was the empty string but however I'm going to use the function load instead of loads. It's more 'effective'
lang-py
f.read()returned an empty string or something that simply isn't JSON.