How to Get Count of JSON Array elements in Python

In this tutorial, we’ll learn how to count or get the length of JSON objects within an array using Python.

We’ll cover methods like using the len() function for a straightforward count, iterating through the array for more detailed analysis, using the reduce() function, and using generator expressions with sum().

 

 

Using len() Function

To count JSON objects in an array in Python, one straightforward method is using the len() function.

This built-in function is a quick way to determine the number of items in a sequence or collection, including lists, which is how JSON arrays are represented in Python.

YOu can use the len() function on the JSON array directly to get its length:

import json
json_data = '''
[
    {"customer_id": 101, "feedback": "Very satisfied"},
    {"customer_id": 102, "feedback": "Satisfied"},
    {"customer_id": 103, "feedback": "Neutral"},
    {"customer_id": 104, "feedback": "Dissatisfied"}
]
'''
data = json.loads(json_data)
count = len(data)
print("Number of JSON objects:", count)

Output:

Number of JSON objects: 4

In this code, we parse this string into a Python list using json.loads().

Then we use len() to get the count of JSON objects in the array, which, in this case, is 4.

 

Iterating through the Array

Another method to count JSON objects in an array in Python is by iterating through the array.

This method allows you to perform additional operations or checks on each object if needed.

Here, we’ll iterate through the array and count the objects:

import json
json_data = '''
[
    {"customer_id": 101, "feedback": "Very satisfied"},
    {"customer_id": 102, "feedback": "Satisfied"},
    {"customer_id": 103, "feedback": "Neutral"},
    {"customer_id": 104, "feedback": "Dissatisfied"}
]
'''
data = json.loads(json_data)
count = 0
for obj in data:
    count += 1
print("Total number of JSON objects:", count)

Output:

Total number of JSON objects: 4

In this script, we iterate through each object in the JSON array. For each object, we increment our count variable and print the customer’s feedback. Finally, we display the total count of objects.

 

Using reduce() Function

The reduce() function, part of the functools module in Python is used to apply a specific function cumulatively to the items of a sequence.

In the following example, we will apply reduce() to count the number of JSON objects in the array:

import json
from functools import reduce
json_data = '''
[
    {"customer_id": 101, "feedback": "Very satisfied"},
    {"customer_id": 102, "feedback": "Satisfied"},
    {"customer_id": 103, "feedback": "Neutral"},
    {"customer_id": 104, "feedback": "Dissatisfied"}
]
'''
data = json.loads(json_data)
def count_objects(accumulator, _):
    return accumulator + 1
count = reduce(count_objects, data, 0)
print("Number of JSON objects:", count)

Output:

Number of JSON objects: 4

In this code snippet, the count_objects function is defined to increment an accumulator each time it is called.

The reduce() function iteratively applies count_objects to each object in the parsed JSON array, starting with an initial accumulator value of 0. The final result is the total count of objects in the array.

 

Using a Generator Expression with sum()

A generator expression combined with the sum() function allows you to count JSON objects in an array in Python.

import json
json_data = '''
[
    {"customer_id": 101, "feedback": "Very satisfied"},
    {"customer_id": 102, "feedback": "Satisfied"},
    {"customer_id": 103, "feedback": "Neutral"},
    {"customer_id": 104, "feedback": "Dissatisfied"}
]
'''
data = json.loads(json_data)
count = sum(1 for _ in data)
print("Number of JSON objects:", count)

Output:

Number of JSON objects: 4

In this snippet, the generator expression (1 for _ in data) creates a sequence of 1s, each representing an object in the JSON array.

The sum() function then adds up these 1s and gets count the number of objects.

 

Length of Nested JSON

Working with deep nested JSON structures requires navigating through the nested structure and counting the elements at the desired level.

Let’s consider a more complex JSON example:

import json
json_data = '''
{
    "region": "North", 
    "feedbacks": [
        {"customer": {"id": 101, "response": {"feedback": "Very satisfied"}}},
        {"customer": {"id": 102, "response": {"feedback": "Satisfied"}}},
        {"customer": {"id": 103, "response": {"feedback": "Neutral"}}},
        {"customer": {"id": 104, "response": {"feedback": "Dissatisfied"}}}
    ]
}
'''
data = json.loads(json_data)
def count_nested_elements(obj, key):
    if isinstance(obj, list):
        return sum(count_nested_elements(item, key) for item in obj)
    elif isinstance(obj, dict):
        if key in obj:
            value = obj[key]
            if isinstance(value, list):
                return len(value)
            else:
                return count_nested_elements(value, key)
        else:
            return 0
    return 1
count = count_nested_elements(data, 'feedbacks')
print("Number of feedback entries:", count)

Output:

Number of feedback entries: 4

In this code, we define a recursive function count_nested_elements that traverses the JSON structure.

It counts the number of times a specified key (‘feedbacks’ in this case) appears in the nested JSON.

 

Conditional Counting

Conditional counting involves counting JSON elements based on certain conditions or criteria.

This method is valuable when you need to count objects that meet specific requirements, such as a particular value for a key.

Let’s count only the positive feedback (“Very satisfied” or “Satisfied”):

import json
json_data = '''
[
    {"customer_id": 101, "feedback": "Very satisfied"},
    {"customer_id": 102, "feedback": "Satisfied"},
    {"customer_id": 103, "feedback": "Neutral"},
    {"customer_id": 104, "feedback": "Dissatisfied"}
]
'''
data = json.loads(json_data)
positive_feedback_count = sum(1 for obj in data if obj['feedback'] in ["Very satisfied", "Satisfied"])
print("Number of positive feedbacks:", positive_feedback_count)

Output:

Number of positive feedbacks: 2

In this script, we use a generator expression with a conditional if clause to filter only those JSON objects where the feedback key has a value of either “Very satisfied” or “Satisfied”.

We then count these filtered objects using the sum() function.

 

Benchmark Test

We will compare the four methods discussed: len(), iteration, reduce(), and sum() with a generator expression.

We’ll use the timeit module to measure the execution time of small code snippets.

Let’s set up a benchmark test using a larger sample JSON array to compare these methods:

import json
from functools import reduce
from timeit import timeit

# Large sample JSON data
json_data = '[' + ','.join(['{"customer_id": ' + str(i) + ', "feedback": "Satisfied"}' for i in range(1000)]) + ']'
data = json.loads(json_data)

# Benchmarking the len() function
len_time = timeit('len(data)', globals=globals(), number=10000)
print("len() execution time:", len_time)

# Benchmarking iteration
iteration_time = timeit('sum(1 for _ in data)', globals=globals(), number=10000)
print("Iteration execution time:", iteration_time)

# Benchmarking reduce()
reduce_time = timeit('reduce(lambda acc, _: acc + 1, data, 0)', globals=globals(), number=10000)
print("reduce() execution time:", reduce_time)

# Benchmarking sum() with a generator expression
sum_time = timeit('sum(1 for _ in data)', globals=globals(), number=10000)
print("sum() with generator expression execution time:", sum_time)

Output:

len() execution time: 0.003174200013745576
Iteration execution time: 11.335025400010636
reduce() execution time: 11.793150800018338
sum() with generator expression execution time: 11.242684199998621

In this benchmark, we’re timing each method over 10,000 iterations to get a reliable measurement.

As you can see, len() function is the fastest method.

Remember, the best method might depend on the context of your application, such as the size of your data and the need for additional operations while counting.

Leave a Reply

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