Open In App

Loop through a JSON array in Python

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A JSON array is an ordered list of values that can store multiple values such as string, number, boolean, or object. The values in a JSON array must be separated by commas and enclosed in squares in brackets []. In this article, we will learn how we can loop through a JSON array in Python.

Iterate over a JSON object in Python

Arrays in JSON are almost the same as arrays in Python. The array index begins with 0 and each value is separated by a comma. JSON arrays can be of multiple data types that is, they can store a string, number, boolean, or even a whole JSON array. Here are some of the benefits of using JSON arrays:

  • They are easy to read and write.
  • They are human-readable and machine-readable.
  • They are a standard format that is supported by many languages and platforms.
  • They are efficient in terms of space and bandwidth.

So, while working with data, JSON arrays are found to be a powerful tool that can help you to store and organize the data in a structured way. Here is an example of a JSON array:

[
  "string",
  123,
  567.36,
  true,
  [
    "Array",
    "In",
    "Array"
  ],
  {
    "name": "John Doe"
  }
]

Looping through a JSON Array in Python

You can loop through a JSON array in Python by using the json module and then iterating through the array using a for loop. Let us see a few examples.

Loop Through JSON data as a String

In this example, we will define the JSON data as a string and load it using the and the load() function to convert the JSON data to a Python object. Then using a for loop we will iterate through the array.

Python3




import json
  
# Sample JSON data
json_data = """
[
    {"id": 1, "name": "John"},
    {"id": 2, "name": "Jane"},
    {"id": 3, "name": "Bob"}
]
"""
  
# Convert JSON data to a Python object
data = json.loads(json_data)
  
# Iterate through the JSON array
for item in data:
    print(item["id"], item["name"])


Output:

1 John
2 Jane
3 Bob

Looping JSON data as an Array

In the same way as above, we can also loop through an array that is being present as a value for a key in a JSON/Dict, we have to only make some of the minor changes as follows:

Python3




import json
  
# Sample JSON data
json_data = """
{
    "sample_data": [
        {"id": 1, "name": "John"},
        {"id": 2, "name": "Jane"},
        {"id": 3, "name": "Bob"}
    ]
}
"""
  
# Convert JSON data to a Python object
data = json.loads(json_data)
  
# Iterate through the array
for item in data["sample_data"]: 
      # Updated data["sample_data"] as the array is 
    # being present as the value for sample_data
    print(item["id"], item["name"])


Output:

1 John
2 Jane
3 Bob

Looping JSON data as a JSON file

In this example, we will be looping through a JSON array whose data is being stored in a JSON file named data.json. Here is the content of the data.json file:

[
    {"id": 1, "name": "John"},
    {"id": 2, "name": "Jane"},
    {"id": 3, "name": "Bob"}
]

Python3




import json
  
# Load the JSON data
with open("data.json") as f:
    data = json.load(f)
  
# Iterate through the JSON array
for item in data:
    print(item["id"], item["name"])


Output:

1 John
2 Jane
3 Bob

Looping JSON data as a Nested JSON Array

In this example, we will be looping through a Nested JSON array. We will go through two loop, the first loop iterates through the main Array while the inner loop iterates through the Array present as the value of nested_array key and prints the value.

Python3




data = [
    {
        "nested_array": [
            {
                "value": "1"
            },
            {
                "value": "2"
            },
            {
                "value": "3"
            }
        ]
    },
    {
        "nested_array": [
            {
                "value": "4"
            },
            {
                "value": "5"
            },
            {
                "value": "6"
            }
        ]
    }
]
  
for item in data:
    for subitem in item["nested_array"]:
        print(subitem["value"])


Output:

1
2
3
4
5
6

You can refer to this article for more information regarding JSON files in Python: Python JSON



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads