Open In App

Filtering Json Response With Python List Comprehension

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JSON (JavaScript Object Notation) is a widely used data interchange format, and working with JSON responses is a common task in many programming scenarios. In Python, list comprehensions provide a concise and efficient way to filter and manipulate data. In this article, we will explore five simple and commonly used methods for filtering JSON responses using Python list comprehensions.

Filtering Json Response With Python List Comprehension

Below, are the methods for Filtering JSON Response With Python List Comprehension in Python.

  • Filtering by Key-Value Pair
  • Filtering by Condition
  • Filtering Nested JSON

Filtering Json Response With Python List Comprehension by Key-Value Pair

Suppose you have a JSON response and you want to filter out specific items based on a key-value pair. Here’s an example using list comprehension: In this example, the list comprehension filters the JSON response to include only the key-value pairs for “name” and “age.”

Python3




import json
 
json_response = '{"name": "John", "age": 25, "city": "New York", "gender": "male"}'
data = json.loads(json_response)
 
filtered_data = {key: value for key, value in data.items() if key in ["name", "age"]}
print(filtered_data)


Output

{'name': 'John', 'age': 25}



Filtering Json Response With Python List Comprehension Using Condition

You might need to filter JSON data based on a specific condition. Here’s an example where we filter out items with age greater than 30: This list comprehension filters the JSON response to include only items where the age is greater than 30.

Python3




import json
 
json_response = '[{"name": "John", "age": 25}, {"name": "Jane", "age": 35}, {"name": "Bob", "age": 28}]'
data = json.loads(json_response)
 
filtered_data = [item for item in data if item["age"] > 30]
print(filtered_data)


Output

[{'name': 'Jane', 'age': 35}]



Filtering Nested JSON With Python List Comprehension

If your JSON response contains nested structures, you can still use list comprehension to filter the data. Here’s an example where we filter based on a nested key: This list comprehension filters the JSON response to include only the “city” key from the nested structure.

Python3




import json
 
json_response = '{"person": {"name": "John", "address": {"city": "New York", "zip": "10001"}}}'
data = json.loads(json_response)
 
filtered_data = {key: data["person"]["address"][key] for key in data["person"]["address"] if key == "city"}
print(filtered_data)


Output

{'city': 'New York'}



Conclusion

In conclusion, Python list comprehensions provide a powerful and concise way to filter JSON responses based on various criteria. By mastering these methods, you can efficiently process and manipulate JSON data in your Python applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads