I have a JSON file and would like to remove a certain book
[
{
"title": "How to love",
"author": "vagabondage3",
"isbn": 9832,
"genre": "Comedy",
"numofcopies": 2
},
{
"title": "How to eat",
"author": "belabor",
"isbn": 2345,
"genre": "Documentary",
"numofcopies": 3
},
{
"title": "hwo",
"author": "TafaTIC",
"isbn": 19,
"genre": "Comedy",
"numofcopies": 9
}
]
Part to remove
{
"title": "hwo",
"author": "TafaTIC",
"isbn": 19,
"genre": "Comedy",
"numofcopies": 9
}
the json file should look like this in the end
[
{
"title": "How to love",
"author": "vagabondage3",
"isbn": 9832,
"genre": "Comedy",
"numofcopies": 2
},
{
"title": "How to eat",
"author": "belabor",
"isbn": 2345,
"genre": "Documentary",
"numofcopies": 3
}
]
I honestly saw a similar example but I can't understand it can you explain please how it was done thanks in advance <3
CodePudding user response:
In the end it depends how you identify which one you want to delete. If you want to delete by index, you could try this:
json_data.pop(2)
Or you could just iterate with a condition:
json_data = [item for item in json_data if item['title'] != 'hwo']
CodePudding user response:
You can do it in multiple ways:
You can use del
operator to remove a certain book by index:
del data_json[2]
Or, You can use pop() method:
data_json.pop(2)