edit json file python code example
Example 1: 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
Example 2: insert json file in python
a_dictionary = {"d": 4}
with open("sample_file.json", "r+") as file:
data = json.load(file) # get data from file
update(a_dictionary)
seek(0)
json.dump(data, file) # insert data in file