modify json file python code example
Example 1: save json file python
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Example 2: edit json file python
import json
with open('data.json', 'r+') as f:
data = json.load(f)
data['id'] = 134 # <--- add `id` value.
f.seek(0) # <--- should reset file position to the beginning.
json.dump(data, f, indent=4)
f.truncate() # remove remaining part