Delete a JSON object from a list in Python

avatar

Borislav Hadzhiev

Last updated: Jul 10, 2022

banner

Photo from Unsplash

Delete a JSON object from a list in Python #

To delete a JSON object from a list:

  1. Parse the JSON object into a Python list of dictionaries.
  2. Use the enumerate() function to iterate over the iterate over the list.
  3. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.
main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_list = json.load(f) # 👇️ [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}] print(my_list) for idx, obj in enumerate(my_list): if obj['id'] == 2: my_list.pop(idx) new_file_name = 'new-file.json' with open(new_file_name, 'w', encoding='utf-8') as f: f.write(json.dumps(my_list, indent=2))

The example shows how to delete a JSON object from an array of objects in a file.

You can use the same approach to delete a JSON object from an array of objects outside of a file.

main.py
import json my_json = '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]' my_list = json.loads(my_json) for idx, dictionary in enumerate(my_list): if dictionary['id'] == 2: my_list.pop(idx) # 👇️ [{'id': 1, 'name': 'Alice'}] print(my_list) json_again = json.dumps(my_list) print(json_again) # 👉️ '[{"id": 1, "name": "Alice"}]'

If your JSON is located in a file, use the json.load() method to parse the json.

The json.load method is used to deserialize a file to a Python object, whereas the json.loads method is used to deserialize a JSON string to a Python object.

The next step is to iterate over the list and check if a key in each dictionary has a specific value.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_list = json.load(f) # 👇️ [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}] print(my_list) for idx, obj in enumerate(my_list): if obj['id'] == 2: my_list.pop(idx) new_file_name = 'new-file.json' with open(new_file_name, 'w', encoding='utf-8') as f: f.write(json.dumps(my_list, indent=2))

Once we find the matching dictionary, we have to use the list.pop() method to remove it from the list.

The list.pop method removes the item at the given position in the list and returns it.

You can also add a break statement if you only want to remove 1 dictionary from the list.
main.py
for idx, obj in enumerate(my_list): if obj['id'] == 2: my_list.pop(idx) break

This saves you some time in needless iterations if the dictionary is towards the beginning of the list.

The last step is to open a new file (new-file.json) in the example, serialize the list to json and write it to the file.

main.py
new_file_name = 'new-file.json' with open(new_file_name, 'w', encoding='utf-8') as f: f.write(json.dumps(my_list, indent=2))
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.