I have a JSON file named 'students.json' which is as follows:-
{
"students": {
"1":{
"name": "Ricky",
"marks": {
"science": "90",
"english": "50"
}
},
"2":{
"name": "Brad",
"marks": {
"science": "80",
"english": "75"
}
}
}
}
I want to update Brad's english marks to 85. All I have is the JSON file, a list of path to marks, and updated marks.
updated_marks = "85"
path_to_marks = ["students", "2", "marks", "english"]
I want to do something like this,
import json
updated_marks = "85"
path_to_marks = ["students", "2", "marks", "english"]
with open('students.json', 'r+') as f:
json_data = json.load(f)
value = json_data
#TODO: code to update marks
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()