Answers for "how to change values in json file using 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
0

replace json data python

with open('file.json', 'r+') as file:
    content = file.read()
    file.seek(0)
    content.replace('string_replaced', 'new_string')
    file.write(content)
Posted by: Guest on July-21-2020

Code answers related to "how to change values in json file using python"

Python Answers by Framework

Browse Popular Code Answers by Language