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 - 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. ... 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"])
Discussions

Advice on how to iterate through JSON to find the first instance of a key value pair?
Is the index sorted in the json? If yes, you can just iterate over the codecs in the json and pick the first one that is audio. for stream in jsondata['streams']: if stream['codec_type']=="audio": cname = stream['codec_name'] break print(cname) More on reddit.com
๐ŸŒ r/learnpython
5
1
September 5, 2023
arrays - Issues iterating through JSON list in Python? - Stack Overflow
for json_data in data['Results']: for attribute, value in json_data.iteritems(): print attribute, value # example usage ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I index an item in a JSON array from Python - Stack Overflow
For instance let's say I wanted to get the position of person 2 from the array in python so position 2 ยท (I looked through websites and previous questions but couldn't figure it out) ... Welcome to SO. Just to be clear you want to find the dictionary (or Json object) with key person2? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Looping through JSON
Hello all, I have a script transform on a dataset that is bound to the instance of a flex repeater: In the first part of the code, I am returning 6 instances with their values from the dataset, now I would like to be able to return an index value for each instance just like the picture below: ... More on forum.inductiveautomation.com
๐ŸŒ forum.inductiveautomation.com
10
0
December 8, 2021
๐ŸŒ
FreeKB
freekb.net โ€บ Article
Python (Scripting) - Loop through JSON list
January 10, 2024 - #!/usr/bin/python3 import json file = open("example.json") try: parsed_json = json.load(file) except Exception as exception: print(f"Got the following exception: {exception}") file.close() print(parsed_json['main']) Running this script should print the following. [{u'foo': u'Hello', u'bar': u'World'}, {u'foo': u'Goodbye', u'bar': u'Earth'}] Here is how you can print a certain item using index numbers.
๐ŸŒ
Tech-otaku
tech-otaku.com โ€บ mac โ€บ using-python-to-loop-through-json-encoded-data
Using Python to Loop Through JSON-Encoded Data โ€“ Tech Otaku
As Python lists are zero-indexed, the first dictionary has an index of 0, the second an index of 1. NOTE: JSON Editor Online is a useful tool that displays JSON-encoded data showing the number of values in a list (JSON array) and each valueโ€™s index within that list.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ advice on how to iterate through json to find the first instance of a key value pair?
r/learnpython on Reddit: Advice on how to iterate through JSON to find the first instance of a key value pair?
September 5, 2023 -

I have an undesirable JSON object that I can't modify:

{
"programs": [

],
"streams": [
    {
        "index": 0,
        "codec_name": "hevc",
        "codec_type": "video"
    },
    {
        "index": 1,
        "codec_name": "aac",
        "codec_type": "audio"
    },
    {
        "index": 2,
        "codec_name": "opus",
        "codec_type": "audio"
    },
    {
        "index": 3,
        "codec_name": "ac3",
        "codec_type": "audio"
    },
    {
        "index": 4,
        "codec_name": "ass",
        "codec_type": "subtitle"
    },
    {
        "index": 5,
        "codec_name": "ttf",
        "codec_type": "attachment"
       }
       ]
}

This is psudo json from the very real output of ffprobe -loglevel error -show_entries format:stream=index,stream,codec_type,codec_name -of json FILENAME

I need to get the first codec_name from the lowest index of the codec_type: audio.

In this case, index 1, 2, and 3 are all of codec_type: audio, so the lowest index/first instance would be 1 and my codec_name would be aac.

Any ideas on how to move forward on a problem like this? I can't seem to find any stackoverflow threads with anything similar.

----------------------------

EDIT, solution here: https://www.reddit.com/r/learnpython/comments/16af8zf/comment/jz74hc9/?utm_source=share&utm_medium=web2x&context=3

Thank you u/shiftybyte, u/djshadesuk!!!

๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ loop through a json array in python
Loop through a JSON array in Python - CodeSpeedy
September 21, 2023 - So how to iterate through a JSON file? 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("---") The output will be the same. ยซ Combine two querysets in Django ยท Control Statement in Python with examples ยป ยท
๐ŸŒ
Quora
quora.com โ€บ How-do-I-iterate-through-JSON-array-object
How to iterate through JSON array object - Quora
Answer (1 of 2): I have the below code which is currently giving me one result. I would like to iterate through the โ€œcommentsโ€ so that end result will returns all the comments. for each(comments in r.orderLines){ // get Collection comments webservice = webServiceFactory.createWebService('
Find elsewhere
๐ŸŒ
Example Code
example-code.com โ€บ python โ€บ json_array_iterate.asp
Iterate over JSON Array containing JSON Objects | Chilkat Examples
sbJsonArray = chilkat.CkStringBuilder() success = sbJsonArray.LoadFile("qa_data/json/arraySample.json","utf-8") arr = chilkat.CkJsonArray() success = arr.LoadSb(sbJsonArray) i = 0 count = arr.get_Size() while i < count : # obj is a CkJsonObject obj = arr.ObjectAt(i) tagId = obj.IntOf("tagId") tagDescription = obj.stringOf("tagDescription") isPublic = obj.BoolOf("isPublic") print("tagId: " + str(tagId)) print("tagDescription: " + tagDescription) print("isPublic: " + str(isPublic)) print("--") i = i + 1
๐ŸŒ
Inductive Automation
forum.inductiveautomation.com โ€บ ignition
Looping through JSON - Ignition - Inductive Automation Forum
December 8, 2021 - Hello all, I have a script transform on a dataset that is bound to the instance of a flex repeater: In the first part of the code, I am returning 6 instances with their values from the dataset, now I would like to be able to return an index value for each instance just like the picture below: ...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python iterate over json array
Python iterate over JSON array - Tutorial - By EyeHunts
June 26, 2023 - import json # Example JSON array json_data = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}, {"name": "Charlie", "age": 35}]' # Parse JSON array into a Python object data = json.loads(json_data) # Iterate over the array for item ...
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?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to iterate through this dictionary (dictionary/list within the dictionary)?
r/learnpython on Reddit: How to iterate through this dictionary (dictionary/list within the dictionary)?
August 16, 2015 -

I've got my dictionary of scraped data here, I was wondering how I can take this and return the data I want from it?

I just want to return the inner dictionary (so 'links, code, cresturl, name, etc') and I'm a bit confused about the structure of this as I haven't found the right question to google...

edit: well this is a stupid question. How do I return the "teams" list/dictionary specifically? It looks like a dictionary within a list because of the syntax [ { } ]

{
    "_links": [
        {
            "self": "http://api.football-data.org/alpha/soccerseasons/354/teams"
        }, 
        {
            "soccerseason": "http://api.football-data.org/alpha/soccerseasons/354"
        }
    ], 
    "count": 20, 
    "teams": [
        {
            "_links": {
                "fixtures": {
                    "href": "http://api.football-data.org/alpha/teams/66/fixtures"
                }, 
                "players": {
                    "href": "http://api.football-data.org/alpha/teams/66/players"
                }, 
                "self": {
                    "href": "http://api.football-data.org/alpha/teams/66"
                }
            }, 
            "code": "MUFC", 
            "crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg", 
            "name": "Manchester United FC", 
            "shortName": "ManU", 
            "squadMarketValue": "425,000,000 \u20ac"
        }, 
        {
            "_links": {
                "fixtures": {
                    "href": "http://api.football-data.org/alpha/teams/72/fixtures"
                }, 
                "players": {
                    "href": "http://api.football-data.org/alpha/teams/72/players"
                }, 
                "self": {
                    "href": "http://api.football-data.org/alpha/teams/72"
                }
            }, 
            "code": "SWA", 
            "crestUrl": "http://upload.wikimedia.org/wikipedia/de/a/ab/Swansea_City_Logo.svg", 
            "name": "Swansea City", 
            "shortName": "Swans", 
            "squadMarketValue": "104,000,000 \u20ac"
        }, 
        {
            "_links": {
                "fixtures": {
                    "href": "http://api.football-data.org/alpha/teams/338/fixtures"
                }, 
                "players": {
                    "href": "http://api.football-data.org/alpha/teams/338/players"
                }, 
                "self": {
                    "href": "http://api.football-data.org/alpha/teams/338"
                }
            }, 
            "code": "LCFC", 
            "crestUrl": "http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png", 
            "name": "Leicester City", 
            "shortName": "Foxes", 
            "squadMarketValue": "63,250,000 \u20ac"
        }, 
        {
            "_links": {
                "fixtures": {
                    "href": "http://api.football-data.org/alpha/teams/62/fixtures"
                }, 
                "players": {
                    "href": "http://api.football-data.org/alpha/teams/62/players"
                }, 
                "self": {
                    "href": "http://api.football-data.org/alpha/teams/62"
                }
            }, 
            "code": "EFC", 
            "crestUrl": "http://upload.wikimedia.org/wikipedia/de/f/f9/Everton_FC.svg", 
            "name": "Everton FC", 
            "shortName": "Everton", 
            "squadMarketValue": "179,250,000 \u20ac"
        },  
        .... etc
         }
      ]
 }
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 53252742 โ€บ how-to-iterate-over-json-array
python - How to iterate over JSON array? - Stack Overflow
# something like this with open("jsonfile.json", "rb") as fp: j = json.load(fp) See the docs here: https://docs.python.org/3.7/library/json.html
๐ŸŒ
Automationedge
community.automationedge.com โ€บ get help & discuss
What is the correct way to iterate through a json list? - Get Help & Discuss - AutomationEdge Community Forum - Robotic Process Automation & IT Automation
Hello, I have as output from a python script a list of dictionaries formatted as json. In each dictionary I have personal information like name and email. I would like to turn each item in that list into a row, so thaโ€ฆ
Published: February 28, 2020
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ iterate through json python
How to Iterate Through JSON Object in Python | Delft Stack
February 2, 2024 - During each iteration, the loop prints out the current key, followed by a colon, and the corresponding value obtained by accessing the dictionary with json_data[key]. Finally, when the code is executed, the output will display the keys and values of the JSON object: ... This output demonstrates a successful iteration through the JSON object, printing each key-value pair in the specified format. The items() method is a powerful feature in Python when working with dictionaries.
๐ŸŒ
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
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.