Loop through a JSON array in Python
Last Updated :
28 Mar, 2024
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
Similar Reads
How to Loop through JSON in EJS ?
EJS stands for Embedded JavaScript. It is a template engine designed to be used with server-side JavaScript environments like NodeJS and It is used to generate dynamic content in a web page. In this article, we will learn about How to Loop through JSON in EJS with different Approaches. Approaches to
4 min read
Working With JSON Data in Python
JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The tex
6 min read
Python | Using for loop in Flask
Prerequisite: HTML Basics, Python Basics, Flask It is not possible to write front-end course every time user make changes in his/her profile. We use a template and it generates code according to the content. Flask is one of the web development frameworks written in Python. Through flask, a loop can
3 min read
Convert class object to JSON in Python
In Python, class objects are used to organize complex information. To save or share this information, we need to convert it into a format like JSON, which is easy to read and write. Since class objects can't be saved directly as JSON, we first convert them into a dictionary (a data structure with ke
3 min read
json.load() in Python
The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json . To use this feature, we import the json package in Pyth
4 min read
How to pass an array to a function in Python
In this article, we will discuss how an array or list can be passed to a function as a parameter in Python. Pass an array to a function in Python So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is
4 min read
How to Parse Data From JSON into Python?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write for machines to parse and generate. Basically it is used to represent data in a specified format to access and work with data easily. Here we will learn, how to create and parse data f
2 min read
Deserialize JSON to Object in Python
Let us see how to deserialize a JSON document into a Python object. Deserialization is the process of decoding the data that is in JSON format into native data type. In Python, deserialization decodes JSON data into a dictionary(data type in python).We will be using these methods of the json module
2 min read
json.loads() in Python
JSON is a lightweight data format used for storing and exchanging data across systems. Python provides a built-in module called json to work with JSON data easily. The json.loads() method of JSON module is used to parse a valid JSON string and convert it into a Python dictionary. In this article, we
4 min read
Convert JSON to dictionary in Python
JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Pytho
4 min read