The idea:
A JSON file should be loaded and the object 2-uID
with its sub-items should be deleted. The edited content should be saved in the same JSON file.
The problem:
I have already tried several approaches, like this one from an SO user, but nothing has worked for me yet.
The code shown does not delete anything, but pushes the content all into one line.
The current code
Python code:
import json
with open("path/to/json") as data_file:
data = json.load(data_file)
for element in data:
if '2-uID' in element:
del element['2-uID']
with open("path/to/json", 'w') as data_file:
data = json.dump(data, data_file)
JSON file:
{
"uID": {
"1-uID": {
"username": "1-username",
"pinned": true
},
"2-uID": {
"username": "2-username",
"pinned": false
},
"3-uID": {
"username": "3-username",
"pinned": false
}
}
}
This is how the JSON file should look like after the process:
{
"uID": {
"1-uID": {
"username": "1-username",
"pinned": true
},
"3-uID": {
"username": "3-username",
"pinned": false
}
}
}