When restaurants is your list, you have to iterate over this key:

for restaurant in data['restaurants']:
    print restaurant['restaurant']['name']
Answer from Daniel on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › loop-through-a-json-array-in-python
Loop through a JSON array in Python - GeeksforGeeks
July 23, 2025 - You can loop through a JSON array in Python by using the json module and then iterating through the array using a for loop.
Discussions

Python Parse JSON array - Stack Overflow
I'm trying to put together a small python script that can parse out array's out of a large data set. I'm looking to pull a few key:values from each object so that I can play with them later on in the More on stackoverflow.com
🌐 stackoverflow.com
Parsing Json with for in loop in python - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Looping through json array in python - Stack Overflow
I have JSON in an array that i am importing into my script "ip_address": [ "192.168.0.1", "192.168.0.2", "192.168.0.3" ] I am loading the JSON and declaring a variable titled ip_address. data = y... More on stackoverflow.com
🌐 stackoverflow.com
May 31, 2017
how to can loop through an array of array in a json object in python - Stack Overflow
import json with open("test/co... for item in data: print(item["languages"]) ... You are missing one essential step, which is parsing the JSON data to Python datastructures. import json # read file f = open("countries.json") # parse JSON to Python datastructures countries = json.load(f) # now you have a list of countries print(type(countries)) # loop through list ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tech-otaku
tech-otaku.com › mac › using-python-to-loop-through-json-encoded-data
Using Python to Loop Through JSON-Encoded Data – Tech Otaku
Each value in the versions array is an object representing a single macOS version. Each object representing a single macOS version contains information on that version in the form of five name/value pairs: family, version, codename, announced and released. All values are strings. To allow Python to access the JSON-encoded data we first need to open the file and then read its contents into memory.
🌐
CodeSpeedy
codespeedy.com › home › loop through a json array in python
Loop through a JSON array in Python - CodeSpeedy
September 21, 2023 - import json with open('sample_json.json', 'r') as json_file: myJSON_array = json.load(json_file) for data in myJSON_array: print("language:", data["language"]) print("duration:", data["duration"]) print("author:", data["author"]) print("site:", data["site"]) print("---")...
🌐
Delft Stack
delftstack.com › home › howto › python › iterate through json python
How to Iterate Through JSON Object in Python | Delft Stack
February 2, 2024 - The expression is then applied to the JSON data using the find() method, and the selected elements are stored in the matches list. Finally, a for loop iterates through the matched elements, printing each one.
Top answer
1 of 2
46

In your for loop statement, Each item in json_array is a dictionary and the dictionary does not have a key store_details. So I modified the program a little bit

import json

input_file = open ('stores-small.json')
json_array = json.load(input_file)
store_list = []

for item in json_array:
    store_details = {"name":None, "city":None}
    store_details['name'] = item['name']
    store_details['city'] = item['city']
    store_list.append(store_details)

print(store_list)
2 of 2
0

If you arrived at this question simply looking for a way to read a json file into memory, then use the built-in json module.

with open(file_path, 'r') as f:
    data = json.load(f)

If you have a json string in memory that needs to be parsed, use json.loads() instead:

data = json.loads(my_json_string)

Either way, now data is converted into a Python data structure (list/dictionary) that may be (deeply) nested and you'll need Python methods to manipulate it.


If you arrived here looking for ways to get values under several keys as in the OP, then the question is about looping over a Python data structure. For a not-so-deeply-nested data structure, the most readable (and possibly the fastest) way is a list / dict comprehension. For example, for the requirement in the OP, a list comprehension does the job.

store_list = [{'name': item['name'], 'city': item['city']} for item in json_array]
# [{'name': 'Mall of America', 'city': 'Bloomington'}, {'name': 'Tempe Marketplace', 'city': 'Tempe'}]

Other types of common data manipulation:

  1. For a nested list where each sub-list is a list of items in the json_array.

    store_list = [[item['name'], item['city']] for item in json_array]
    # [['Mall of America', 'Bloomington'], ['Tempe Marketplace', 'Tempe']]
    
  2. For a dictionary of lists where each key-value pair is a category-values in the json_array.

    store_data = {'name': [], 'city': []}
    for item in json_array:
        store_data['name'].append(item['name'])
        store_data['city'].append(item['city'])
    # {'name': ['Mall of America', 'Tempe Marketplace'], 'city': ['Bloomington', 'Tempe']}
    
  3. For a "transposed" nested list where each sub-list is a "category" in json_array.

    store_list = list(store_data.values())
    # [['Mall of America', 'Tempe Marketplace'], ['Bloomington', 'Tempe']]
    
🌐
Iditect
iditect.com › faq › python › looping-through-a-json-array-in-python.html
Looping through a JSON array in Python
In this example, the json.loads() function is used to parse the JSON array into a Python list of dictionaries. Then, the for loop iterates through each dictionary in the list, accessing the values using the keys 'name' and 'age'.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › loop through json python
Loop through JSON Python
June 26, 2023 - It involves converting the JSON data into a Python object (such as a list or a dictionary) using the json module, and then using loops to traverse and process the JSON data. Here’s the syntax for looping through JSON data in Python: import ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-through-nested-json-object-using-python
Iterate Through Nested Json Object using Python - GeeksforGeeks
July 23, 2025 - In this example, the Python code defines a function, `iterate_nested_json_for_loop`, which uses a for loop to recursively iterate through a nested JSON object and print key-value pairs.
🌐
Stack Overflow
stackoverflow.com › questions › 53937565 › parsing-json-with-for-in-loop-in-python
Parsing Json with for in loop in python - Stack Overflow
I have the following sample code which parses JSON file from API for query in r["queries"]: print(query["attributes"]["planning_wait_time"]) This gives me result: 5986 5987 42 0 63 5978 Trace...
Top answer
1 of 3
4

The reason why it's printing individual numbers is because the address is a string. Hence, it's not really each number that's being printed, but rather each letter of a string. Consider:

word = "abc"
for letter in word:
    print(letter)

# prints:
# a
# b
# c

Therefore, it means somewhere you're assigning individual IP addresses to a variable, and then iterate through that variable (which is a string). Without you providing more code on how you get the ip_address variable, it's hard to say where the problem is.

One way to print your IP addresses (assuming you have them in a dict):

addresses = {"ip_address": [
    "192.168.0.1",
    "192.168.0.2",
    "192.168.0.3"
]}

for address in addresses["ip_address"]:  # this gets you a list of IPs
      print(address)

Even if you have them somewhere else, the key insight to take away is to not iterate over strings, as you'll get characters (unless that's what you want).

Updated to address edits

Since I do not have the file (is it a file?) you are loading, I will assume I have exact string you posted. This is how you print each individual address with the data you have provided. Note that your situation might be slightly different, because, well, I do not know the full code.

# the string inside load() emulates your message
data = yaml.load('"ip_address": ["192.168.0.1", "192.168.0.2", "192.168.0.3"]')
ip_addresses = data.get('ip_address')

for address in ip_addresses: 
    print(address)
2 of 3
2

In your case ip_address = '192.168.0.1'

Are you sure you have the right value in ip_address?

🌐
Digitaldesignjournal
digitaldesignjournal.com › how-to-use-for-loop-in-json-python
How to use for loop in JSON Python?
October 3, 2023 - Then, we use a for loop to iterate over the items of the dictionary. The items() method returns a sequence of key-value pairs, allowing us to access both the key and value within the loop.
🌐
Python Guides
pythonguides.com › json-data-in-python
How To Get Values From A JSON Array In Python?
November 29, 2024 - In this tutorial, I explained how to get values from a JSON array using Python. We learned how to parse JSON data, access values using loops and list comprehension, handle nested arrays, and filter data based on conditions.
🌐
Stack Overflow
stackoverflow.com › questions › 71498490 › how-to-can-loop-through-an-array-of-array-in-a-json-object-in-python
how to can loop through an array of array in a json object in python - Stack Overflow
import json # read file f = open("countries.json") # parse JSON to Python datastructures countries = json.load(f) # now you have a list of countries print(type(countries)) # loop through list of countries for country in countries: # you can access languages with country["languages"]; JSON objects are Python dictionaries now print(type(country)) for language in country["languages"]: print(language) f.close() Expected output: <class 'list'> <class 'dict'> Pashto Uzbek Turkmen ...
🌐
Quora
quora.com › How-do-I-loop-through-a-JSON-file-with-multiple-keys-sub-keys-in-Python
How to loop through a JSON file with multiple keys/sub-keys in Python - Quora
October 13, 2022 - Answer (1 of 4): You have to do it recursively. Here is an example. [code]a_dict = {'a': 'a value', 'b': {'b.1': 'b.1 value'}} def iterate(dictionary): for key, value in dictionary.items(): if isinstance(value, dict): iterate(value) continue print('key {...
🌐
Example Code
example-code.com › python › json_array_iterate.asp
CkPython Iterate over JSON Array containing JSON Objects
March 23, 2023 - Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • ...
🌐
EyeHunts
tutorial.eyehunts.com › home › python iterate over json array
Python iterate over JSON array - Tutorial - By EyeHunts
January 19, 2022 - Here’s the syntax for iterating ... 30}, {"name": "Charlie", "age": 35}]' # Parse JSON array into a Python object data = json.loads(json_data) # Iterate over the array for item in data: # Access properties of each object name ...
🌐
Reddit
reddit.com › r/learnpython › how do i loop through a nested object from a json file?
r/learnpython on Reddit: How do I loop through a nested object from a JSON file?
January 26, 2022 -

Hi there, I am trying to read through a package.json file which is below. I want to read in specifically the dependencies and add them to key, value variables. I can't figure out how to read in nested object. If anyone can give me any tips?

{
  "name": "untitled",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "express": "https://github.com/IAmAndyIE/expressjs.com.git"
  },
}

My current code is below.

import json


def test_document():
    f = open('../untitled/package.json')

    data = json.load(f)

    for key, values in data.items():
        if key == "dependencies":
            print(values)

    f.close()


if __name__ == '__main__':
    test_document()

Thanks

🌐
freeCodeCamp
forum.freecodecamp.org › python
Help Iterate Python - Json file - Python - The freeCodeCamp Forum
November 28, 2020 - Can you help me iterate over this Json file ?to have the ‘name’ { “data”: [ { “circulating_supply”: 18556575, “cmc_rank”: 1, “date_added”: “2013-04-28T00:00:00.000Z”, “id”: 1, “last_updated”: “2020-11-28T10:13:02.000Z”, “max_supply”: 21000000, “name”: ...