Last updated: Apr 10, 2024
Reading timeยท5 min
for
loopfilter()
functionTo filter a JSON array in Python:
json.loads()
method to convert the JSON array to a Python list.import json json_array = json.dumps( [ {'name': 'Alice', 'salary': 100}, {'name': 'Bobby', 'salary': 50}, {'name': 'Carl', 'salary': 75} ] ) a_list = json.loads(json_array) filtered_list = [ dictionary for dictionary in a_list if dictionary['salary'] > 50 ] # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Carl', 'salary': 75}] print(filtered_list)
The json.dumps() method converts a Python object to a JSON formatted string.
Conversely, the json.loads() method parses a JSON string into a native Python object.
json.loads()
method to convert the JSON array to a native Python list.We then used a list comprehension to iterate over the list.
On each iteration, we check if a certain condition is met and return the result.
The code sample checks if each dictionary has a salary
key with a value
greater than 50
.
import json json_array = json.dumps( [ {'name': 'Alice', 'salary': 100}, {'name': 'Bobby', 'salary': 50}, {'name': 'Carl', 'salary': 75} ] ) a_list = json.loads(json_array) filtered_list = [ dictionary for dictionary in a_list if dictionary['salary'] > 50 ] # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Carl', 'salary': 75}] print(filtered_list)
The new list only contains the dictionaries that meet the condition.
You can use this approach to check for any condition.
Alternatively, you can use a for loop.
for
loopThis is a four-step process:
json.loads()
method to convert the JSON array to a Python list.for
loop to iterate over the list.import json json_array = json.dumps( [ {'name': 'Alice', 'salary': 100}, {'name': 'Bobby', 'salary': 50}, {'name': 'Carl', 'salary': 75} ] ) a_list = json.loads(json_array) filtered_list = [] for dictionary in a_list: if dictionary['salary'] > 50: filtered_list.append(dictionary) # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Carl', 'salary': 75}] print(filtered_list)
We used a for
loop to iterate over the list.
On each iteration, we check if the current dictionary has a salary
key with a
value greater than 50
.
If the condition is met, we use the list.append()
method to
append the dictionary to a new list.
The list.append() method adds an item to the end of the list.
The new list only contains the items of the original list that meet the condition.
You can use the same approach to filter a JSON array stored in a file.
To filter a JSON array that is stored in a file:
JSON.load()
method to deserialize the file to a Python list.import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: a_list = json.load(f) # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Bobby', 'salary': 50}, {'name': 'Carl', 'salary': 75}] print(a_list) filtered_list = [ dictionary for dictionary in a_list if dictionary['salary'] > 50 ] # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Carl', 'salary': 75}] print(filtered_list)
The code sample assumes that you have an example.json
file stored in the same
directory as your main.py
script.
[ {"name": "Alice", "salary": 100}, {"name": "Bobby", "salary": 50}, {"name": "Carl", "salary": 75} ]
The json.load() method is used to deserialize a file to a Python object.
On the other hand, the json.loads() method is used to deserialize a JSON string to a Python object.
json.load()
method expects a text file or a binary file containing a JSON document that implements a .read()
method.Once we have the data from the JSON file parsed to a native Python list, we can
use a list comprehension or a for
loop to filter the list.
You can also use the filter()
function to filter a JSON array.
filter()
functionThis is a three-step process:
json.loads()
function to parse the JSON array into a Python list.lambda
function and the list to the filter()
function.lambda
function should check if each list item meets a condition.import json json_array = json.dumps( [ {'name': 'Alice', 'salary': 100}, {'name': 'Bobby', 'salary': 50}, {'name': 'Carl', 'salary': 75} ] ) a_list = json.loads(json_array) filtered_list = list( filter( lambda dictionary: dictionary['salary'] > 50, a_list ) ) # ๐๏ธ [{'name': 'Alice', 'salary': 100}, {'name': 'Carl', 'salary': 75}] print(filtered_list)
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
lambda
function we passed to filter()
gets called with each dictionary from the list.The function checks if the dictionary meets a certain condition and returns the result.
The last step is to use the list() class to
convert the filter
object to a list.
You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path