How To Filter JSON Data In Python

Working with JSON data in Python is a common task in the context of web scraping, data analysis, and application development. It is essential to know how to filter JSON data to extract only the relevant information you need. In this tutorial, we will learn how to filter JSON data by keys or values using the Python programming language.

Python Libraries for JSON:

Python has a built-in package called json for encoding and decoding JSON data. The package provides two methods: json.loads() to parse a JSON string and json.load() to parse a JSON file. We will use these methods along with Python’s dictionaries and list comprehensions to filter JSON data efficiently.

Step 1: Import json library and load JSON data

First, import the json library and load a JSON string containing the data you want to filter. Here, we have a JSON string representing a list of people and their relevant data:

This code prints the following output:

[
    {'name': 'Alice', 'age': 30, 'city': 'New York'},
    {'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
    {'name': 'Charlie', 'age': 35, 'city': 'New York'},
    {'name': 'David', 'age': 25, 'city': 'Los Angeles'}
]

Step 2: Filter JSON data by key

Now that we have the JSON data, let’s filter it by a specific key. In this example, we will filter the list of people by their age. We will use list comprehension to achieve this:

The output contains only the people with the specified age:

[
    {'name': 'Bob', 'age': 25, 'city': 'San Francisco'},
    {'name': 'David', 'age': 25, 'city': 'Los Angeles'}
]

Step 3: Filter JSON data by value

Let’s say you want to filter the data based on a specific city. You can use list comprehension again, this time with the city as the filter:

The output contains only the people living in the specified city:

[
    {'name': 'Alice', 'age': 30, 'city': 'New York'},
    {'name': 'Charlie', 'age': 35, 'city': 'New York'}
]

Full Code

Conclusion

In this tutorial, we learned how to filter JSON data in Python using the built-in json library and list comprehensions. Filtering JSON data is a crucial skill, as it enables you to efficiently process and analyze large sets of data.

With the techniques shown in this tutorial, you can easily filter JSON data by keys or values and streamline your data processing workflow.