Reputation: 579
I generate a large nested dictionary and then save it as a json, then later on I try to load it (so that I can append more data to the json). However I am constantly getting an error on the following line of code:
# to save
with open(json_path,'w') as f:
json.dump(data, f, indent=3, sort_keys=True)
# to load
with open(json_path,'r') as f:
data = json.load(f)
Errors I have seen so far:
ValueError: end is out of bounds
ValueError: expecting object ...
ValueError: unterminated string ...
But the strange thing is: if I pdb after the exception and try to load the json again, it works everytime!
Upvotes: 0
Views: 608
Reputation: 1122182
You are reading too early, and the write hasn't completed yet. If you are using separate processes or threads, you'll need to make sure you serialise read and write operations.
Use a lock to make sure the write operation completes before you attempt to read.
Upvotes: 1