1import json
2
3with open('data.json', 'r+') as f:
4 data = json.load(f)
5 data['id'] = 134 # <--- add `id` value.
6 f.seek(0) # <--- should reset file position to the beginning.
7 json.dump(data, f, indent=4)
8 f.truncate() # remove remaining part
9
1a_dictionary = {"d": 4}
2
3with open("sample_file.json", "r+") as file:
4 data = json.load(file) # get data from file
5 update(a_dictionary)
6 seek(0)
7 json.dump(data, file) # insert data in file