![]() |
VOOZH | about |
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for storing and exchanging data between a server and a web application. Python, with its built-in json module, provides powerful tools for working with JSON data. In this article, we'll explore how to filter and manipulate JSON data using Python, focusing on five commonly encountered scenarios.
Below, are the methods of Python JSON Data Filtering in Python.
In this example, the below code uses the `JSON` module in Python to load a JSON-formatted string representing personal data. It then filters the data by selecting only the "name" and "age" keys, creating a new dictionary called `filtered_data`, and printing the result.
{'name': 'John', 'age': 30}In this example, the below code utilizes Python's `json` module to load a JSON array representing people's data. It then filters the array to retain only those individuals whose age is greater than 30, creating a new list called `filtered_people.
[{'name': 'Bob', 'age': 35}]In this example, the below code employs Python's `json` module to load a nested JSON object representing book data. It then filters the nested structure to extract the author's age and prints the result.
The author's age is 40
In this example, in below code `json` module is used to load a JSON array containing product information. The code then performs conditional filtering to create a new list, `in_stock_products`, including only those products with stock quantities greater than 5.
[{'name': 'Mouse', 'price': 20, 'stock': 10}]