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.

Chris Cris
  • 215
  • 3
  • 13
  • possible duplicate of [Reading a JSON file using Python](http://stackoverflow.com/questions/20199126/reading-a-json-file-using-python) – Andy Mar 25 '15 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. – Martijn Pieters Mar 25 '15 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. – Chris Cris Mar 25 '15 at 14:03

1 Answers1

1

Try

with open('data.json', 'w') as f:
    f.write(json.dumps(data))

with open('data.json', 'r') as f:
    json.load(f)
cangoektas
  • 679
  • 5
  • 7
  • the problem was the empty string but however I'm going to use the function load instead of loads. It's more 'effective' – Chris Cris Mar 25 '15 at 14:04