I want to be able to check if a json file has the top level key I'm looking for and delete that nested dictionary.
Json file:
{
"One": {
"action": "One",
"delay": 1559243024.3665395,
"seconds": 0,
"score": 0,
"delta": 1559183024.3665395,
"grace_sec": 60000
},
"Two": {
"action": "Two",
"delay": 1559321009.969849,
"seconds": 0,
"score": 14,
"delta": 1559261009.969849,
"grace_sec": 60000
},
"Three": {
"action": "Three",
"delay": 1559909745.5981774,
"seconds": 0,
"score": 0,
"delta": 1559309745.5981774,
"grace_sec": 600000
},
"Four": {
"action": "Four",
"delay": 1559909757.0363235,
"seconds": 0,
"score": 1,
"delta": 1559309757.0363235,
"grace_sec": 600000
}
}
This is what I have tried so for but hasn't worked:
if name == child.text:
...
with open("streak.json", "r+") as f:
data = json.load(f)
for x in data:
if name in x:
del x[name]
with open("streak.json", "w") as file:
data = json.dump(data, file)
So for instance if the name == "Two", then all of Two's key value pairs would be deleted including Two itself.