How To Modify Nested Json With Python
I need to update (CRUD) a nested JSON file using Python. To be able to call python function(s)(to update/delete/create) entires and write it back to the json file. Here is a sample
Solution 1:
I feel like I'm missing something in your question. In any event, what I understand is that you want to read a json file, edit the data as a python object, then write it back out with the updated data?
Read the json file:
import json
f = open("data.json")
raw_data = f.read()
f.close()
data = json.loads(raw_data)
That creates a dictionary (given the format you've given) that you can manipulate however you want. Assuming you want to write it out:
json_data = json.dumps(data)
f = open("data.json","w")
f.write(json_data)
f.close()
Post a Comment for "How To Modify Nested Json With Python"