To parse and modify a JSON file in Python, you can use the built-in json
module.
Here’s an example of how to load a JSON file, modify it, and save it back to a file:
import json
# Load the JSON file
with open('example.json', 'r') as f:
data = json.load(f)
# Modify the data
data['name'] = 'John Doe'
data['age'] = 30
# Save the modified data back to the file
with open('example.json', 'w') as f:
json.dump(data, f)
In this example, we first load the JSON file example.json
using the json.load()
method. We then modify the name
and age
keys in the data dictionary. Finally, we save the modified data back to the same file using the json.dump()
method.
+ There are no comments
Add yours