Answers for "edit json file contents python"

14

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
Posted by: Guest on December-09-2020

Python Answers by Framework

Browse Popular Code Answers by Language