In this Python tutorial, we will learn about different ways to iterate over a JSON object.
Agenda
Introduction
JSON stands for Javascript object notation. Using JSON, we can store the data in key-value pair format. The main advantage of JSON is we can easily understand the data.
JSON Structure:
{ string: value, string: value,.............. }
Let’s see the ways to iterate over a JSON object.
Iterate over JSON Object using the loads() method with for loop
In this scenario,
- we are going to create a JSON string and load it into a dictionary using the loads() method.
Syntax:
Frequently Asked:
loaded = json.loads(input_json_string)
where input_json_string is the JSON string or object
- Iterate that dictionary (loaded) using for loop with an iterator.
Syntax:
for iterator in loaded: print(iterator, ":", loaded[iterator])
where the iterator is used to iterate the keys in a dictionary. Let’s see the example, to understand it better.
In this example, we created a JSON string with 5 elements and iterate using for loop.
# import JSON module import json # Consider the json string with 5 values input_json_string = '{ "tutorial-1": "python", \ "tutorial-2": "c++", \ "tutorial-3": "pandas", \ "tutorial-4": "numpy", \ "tutorial-5": ".net"}' # Load input_json_string into a dictionary-loaded loaded = json.loads(input_json_string) # Loop along dictionary keys for iterator in loaded: print(iterator, ":", loaded[iterator])
Output:
tutorial-1 : python tutorial-2 : c++ tutorial-3 : pandas tutorial-4 : numpy tutorial-5 : .net
From the output, we can see that all the values present in JSON are iterated.
Example 2:
If there are multiple values attached to a string element, the loads() method works fine. Let’s see how to iterate over all values in a list.
import json # Consider the json string with 5 key value pairs, where each value is a list input_json_string = '{ "tutorial-1": ["subject1","subject2","subject3"], \ "tutorial-2": ["subject1","subject2","subject3"], \ "tutorial-3": ["subject1","subject2","subject3"], \ "tutorial-4": ["subject1","subject2","subject3"], \ "tutorial-5": ["subject1","subject2","subject3"] }' # Load input_json_string into a dictionary-loaded loaded = json.loads(input_json_string) # Loop along dictionary keys for iterator in loaded: print(iterator, ":", loaded[iterator])
Output:
tutorial-1 : ['subject1', 'subject2', 'subject3'] tutorial-2 : ['subject1', 'subject2', 'subject3'] tutorial-3 : ['subject1', 'subject2', 'subject3'] tutorial-4 : ['subject1', 'subject2', 'subject3'] tutorial-5 : ['subject1', 'subject2', 'subject3']
In the above code, we assigned 5 values for all string elements and iterated using for loop.
Example 3:
Here, we consider the json string with 2 string elements and with 3 key-value pairs. Load them in a dictionary and iterate using for loop.
import json # consider the json string with 2 string elements # with 3 key-value pairs through a dictionary input_json_string = '{"tutorial-1": {"subject1":"python", \ "subject2":"php", \ "subject3":"node.js"}, \ "tutorial-2": {"subject1":"java", \ "subject2":"android", \ "subject3":"css" } }' # Load input_json_string into a dictionary-loaded loaded = json.loads(input_json_string) #Loop along dictionary keys for iterator in loaded: print(iterator, ":", loaded[iterator])
Output:
tutorial-1 : {'subject1': 'python', 'subject2': 'php', 'subject3': 'node.js'} tutorial-2 : {'subject1': 'java', 'subject2': 'android', 'subject3': 'css'}
Iterate over JSON file using load() and for loop
Here, the json string is available in a file and we have to open that file and access the json string from it.
Step 1: Open the file.
By using the open() method, we can open the file along ‘with’ keyword
Syntax:
with open('file_name.json') as value:
where, file_name is the name of the json file where, json data is stored.
Step 2: Load the json string into a variable
json.load(value)
Step 3: Iterate that dictionary using for loop with an iterator.
Syntax:
for iterator in loaded: print(iterator, ":", loaded[iterator])
Example 1:
In this example, we placed a json string with 5 elements in a file named – tutorial1.json and then load into a dictionary and iterate using for loop.
JSON string in file: tutorial1.json
{"tutorial-1": "python", "tutorial-2": "c++", "tutorial-3": "pandas", "tutorial-4": "numpy","tutorial-5": ".net"}
Code to load json string from file and then iterating over it is as follows,
import json # load the json file with open('tutorial1.json') as value: #load each element using load() function dictionary = json.load(value) # iterate the dictionary for iterator in dictionary: print(iterator, ":", dictionary[iterator])
Output:
tutorial-1 : python tutorial-2 : c++ tutorial-3 : pandas tutorial-4 : numpy tutorial-5 : .net
From the output, we can see that all the values present in JSON file are iterated.
Example 2:
In this example, we placed a json string in a file named – tutorial.json. JSON String has with 5 elements, that have 5 values each. Then we loaded json string into a dictionary and iterated using for loop.
JSON string in file
{"tutorial-1": ["subject1","subject2","subject3"], "tutorial-2": ["subject1","subject2","subject3"], "tutorial-3": ["subject1","subject2","subject3"], "tutorial-4": ["subject1","subject2","subject3"], "tutorial-5": ["subject1","subject2","subject3"]}
Code to load json string from file and then iterating over it is as follows,
import json #load the json file with open('tutorial.json') as value: # load each element using load() function dictionary = json.load(value) # iterate the dictionary for iterator in dictionary: print(iterator, ":", dictionary[iterator])
Output:
tutorial-1 : ['subject1', 'subject2', 'subject3'] tutorial-2 : ['subject1', 'subject2', 'subject3'] tutorial-3 : ['subject1', 'subject2', 'subject3'] tutorial-4 : ['subject1', 'subject2', 'subject3'] tutorial-5 : ['subject1', 'subject2', 'subject3']
From the output, we can see that all the values present in JSON file are iterated.
Example 3: In this example, we will consider the json string with 2 string elements and with 3 key-value pairs in each of them. We will load it in a dictionary and iterate using for loop.
JSON string in file
{"tutorial-1": {"subject1":"python","subject2":"php","subject3":"node.js"}, "tutorial-2": {"subject1":"java","subject2":"android","subject3":"css"}}
Code to load json string from file and then iterating over it is as follows,
import json #load the json file with open('tutorial.json') as value: #load each element using load() function dictionary = json.load(value) #iterate the dictionary for iterator in dictionary: print(iterator, ":", dictionary[iterator])
Output:
tutorial-1 : {'subject1': 'python', 'subject2': 'php', 'subject3': 'node.js'} tutorial-2 : {'subject1': 'java', 'subject2': 'android', 'subject3': 'css'}
From the output, we can see that all the key-value pairs within the string element present in JSON file are iterated.
Summary
From this tutorial, we learned about two ways to iterate a JSON object using the load() method and for loop. So based on the need, you can use the above-discussed methods. Happy Learning.