How To Remove Null Values from JSON in Python
In this tutorial, you’ll learn various methods to remove Null values from JSON in Python
We’ll explore multiple methods, including dictionary comprehension, iteration & deletion, and handling nested JSON.
Using Dictionary Comprehension
Imagine you have a JSON object that has some null values, which you need to remove:
import json json_data = '{"customer_id": 12345, "name": "Jordan", "email": null, "phone": "555-1234", "address": null}' data_dict = json.loads(json_data)
Output:
{'customer_id': 12345, 'name': 'Jordan', 'email': None, 'phone': '555-1234', 'address': None}
Here, we convert the JSON string to a dictionary, where None
represents null values.
Next, use dictionary comprehension to remove null values:
cleaned_data = {k: v for k, v in data_dict.items() if v is not None} print(cleaned_data)
Output:
{'customer_id': 12345, 'name': 'Jordan', 'phone': '555-1234'}
In this step, the comprehension iterates over each key-value pair in the dictionary, including them only if the value is not None
.
Iteration and Deletion
Assume you have the same initial JSON object:
data_dict = {'customer_id': 12345, 'name': 'Jordan', 'email': None, 'phone': '555-1234', 'address': None}
To remove null values by iterating and deleting, you can do the following:
for key in list(data_dict.keys()): if data_dict[key] is None: del data_dict[key] print(data_dict)
Output:
{'customer_id': 12345, 'name': 'Jordan', 'phone': '555-1234'}
In this code, we create a list of the dictionary’s keys and iterate over it. If a value associated with a key is None
, we delete that key-value pair.
Using list(data_dict.keys())
is important to avoid a runtime error that occurs when the size of the dictionary changes during iteration.
Remove Null from Nested JSON
Let’s consider a more complex JSON structure with nested elements:
nested_json_data = { "customer_id": 12345, "name": "Jordan", "contact_info": { "email": None, "phone": "555-1234", "social_media": { "facebook": None, "twitter": "@jordan123" } }, "addresses": [None, {"street": "123 Elm St", "city": "Springfield"}] }
To remove null values from this nested structure, you’ll need a function that can recursively handle dictionaries and lists:
def remove_nulls(value): if isinstance(value, dict): return {k: remove_nulls(v) for k, v in value.items() if v is not None} elif isinstance(value, list): return [remove_nulls(item) for item in value if item is not None] else: return value cleaned_data = remove_nulls(nested_json_data) print(cleaned_data)
Output:
{ 'customer_id': 12345, 'name': 'Jordan', 'contact_info': { 'phone': '555-1234', 'social_media': {'twitter': '@jordan123'} }, 'addresses': [{'street': '123 Elm St', 'city': 'Springfield'}] }
In this function, remove_nulls
, we check if the input is a dictionary or a list.
If it’s a dictionary, we use dictionary comprehension to remove null values, and if it’s a list, we use a list comprehension.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.