How To Sort JSON Arrays in Python

In this tutorial, you’ll learn various methods of sorting JSON arrays in Python.

From basic sorting techniques to handling nested arrays and sorting by multiple criteria and more.

 

 

Using sorted()

In Python, the sorted() function allows you to sort iterables such as lists.

Imagine you have a JSON array and you need to sort this array based on a specific condition:

import json
json_data = '''
[
    {"name": "Customer A", "id": 102, "usage": 450},
    {"name": "Customer B", "id": 101, "usage": 300},
    {"name": "Customer C", "id": 103, "usage": 600}
]
'''
data = json.loads(json_data)
sorted_data = sorted(data, key=lambda x: x['usage'])
print(sorted_data)

Output:

[
    {'name': 'Customer B', 'id': 101, 'usage': 300},
    {'name': 'Customer A', 'id': 102, 'usage': 450},
    {'name': 'Customer C', 'id': 103, 'usage': 600}
]

The lambda function in the sorted() method acts as a key, it makes the sorting process by the ‘usage’ value.

Notice how ‘Customer B’ with the lowest usage appears first.

 

Sorting By Multiple Fields

You can use a custom function with sorted() function to sort by multiple fields such as the combination of ‘data_limit’ and ‘price’.

Suppose you want a plan with a higher data limit but lower price should be ranked higher.

import json
json_data = '''
[
    {"plan": "Plan A", "data_limit": 50, "price": 20},
    {"plan": "Plan B", "data_limit": 75, "price": 25},
    {"plan": "Plan C", "data_limit": 50, "price": 15}
]
'''
plans = json.loads(json_data)
def compare_plans(plan):
    return -plan['data_limit'], plan['price']
sorted_plans = sorted(plans, key=compare_plans)
print(sorted_plans)

Output:

[
    {'plan': 'Plan B', 'data_limit': 75, 'price': 25},
    {'plan': 'Plan C', 'data_limit': 50, 'price': 15},
    {'plan': 'Plan A', 'data_limit': 50, 'price': 20}
]

In this output, ‘Plan B’ is first because it offers the highest data limit. Among ‘Plan A’ and ‘Plan C’, which have the same data limit, ‘Plan C’ is preferred due to its lower price.

The negative sign in -plan['data_limit'] ensures a descending order for data limit, while the ascending order of price is maintained.

 

Sorting in Descending Order

By setting the reverse parameter to True, the sorting order is reversed.

Let’s say you want to sort the above JSON array data based on ‘download_speed’, in descending order to quickly identify the highest speeds.

import json
json_data = '''
[
    {"network": "Network A", "download_speed": 50},
    {"network": "Network B", "download_speed": 75},
    {"network": "Network C", "download_speed": 65}
]
'''
networks = json.loads(json_data)
sorted_networks = sorted(networks, key=lambda x: x['download_speed'], reverse=True)
print(sorted_networks)

Output:

[
    {'network': 'Network B', 'download_speed': 75},
    {'network': 'Network C', 'download_speed': 65},
    {'network': 'Network A', 'download_speed': 50}
]

In this output, the networks are sorted by their download speed, with the highest speed at the top.

 

Using array.sort()

The array.sort() method in Python is an in-place sorting technique, meaning it modifies the array it is applied to.

This method is useful when you want to sort an array without creating a new sorted array.

Suppose you have a JSON array of customer feedback scores and you want to sort this array by ‘score’.

import json
json_data = '''
[
    {"customer_id": 1001, "score": 8},
    {"customer_id": 1002, "score": 9},
    {"customer_id": 1003, "score": 7}
]
'''
feedback_scores = json.loads(json_data)
feedback_scores.sort(key=lambda x: x['score'])
print(feedback_scores)

Output:

[
    {'customer_id': 1003, 'score': 7},
    {'customer_id': 1001, 'score': 8},
    {'customer_id': 1002, 'score': 9}
]

In this output, the feedback scores are sorted in ascending order. Unlike sorted(), array.sort() changes the original feedback_scores list.

 

Sort Nested JSON Array

Let’s consider an example where you need to sort a nested array of data usage records within each customer’s record.

import json
json_data = '''
[
    {
        "customer_id": 1001,
        "usage_records": [
            {"date": "2023-12-20", "data_used": 4.5},
            {"date": "2023-12-21", "data_used": 3.0}
        ]
    },
    {
        "customer_id": 1002,
        "usage_records": [
            {"date": "2023-12-20", "data_used": 8.5},
            {"date": "2023-12-21", "data_used": 2.0}
        ]
    }
]
'''
customers = json.loads(json_data)
for customer in customers:
    customer['usage_records'].sort(key=lambda x: x['data_used'])
print(customers)

Output:

[
    {
        'customer_id': 1001, 
        'usage_records': [
            {'date': '2023-12-20', 'data_used': 3.0}, 
            {'date': '2023-12-21', 'data_used': 4.5}
        ]
    }, 
    {
        'customer_id': 1002, 
        'usage_records': [
            {'date': '2023-12-20', 'data_used': 2.0}, 
            {'date': '2023-12-21', 'data_used': 8.5}
        ]
    }
]

In this output, the nested ‘usage_records’ for each customer are sorted by ‘data_used’ in ascending order.

Here we iterate through each customer and apply the sort method to their ‘usage_records’ array, based on the ‘data_used’ key.

Leave a Reply

Your email address will not be published. Required fields are marked *