0

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
3
  • possible duplicate of Reading a JSON file using Python Commented Mar 25, 2015 at 13:55
  • Your code as posted here doesn't throw that error. That would only happen if f.read() returned an empty string or something that simply isn't JSON. Commented Mar 25, 2015 at 13:55
  • Ok the problem was the empty string....I did not managed that an other method were deleting the content of the file. Commented Mar 25, 2015 at 14:03

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

the problem was the empty string but however I'm going to use the function load instead of loads. It's more 'effective'

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.