Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through array of json objects

What is the correct way to loop through the following json object?

test = [{
    'start': 'ieo5',
    'end': 'tiu9',
    'chain': 10489
}, {
    'start': 'qvc5',
    'end': 'tiu9',
    'chain': 45214
}, {
    'start': 'ieo5',
    'end': 'tiu9',
    'chain': 69296
}]

I essentially want to loop through and print out whatever the value of start is.

I've tried a bunch of options like the ones listed here but can't seem to get it to work.

This doesn't work:

for x in test
    print x['start'] 
like image 240
Tony Scialo Avatar asked Sep 18 '25 07:09

Tony Scialo


1 Answers

Your code logic works fine, just few things making it not work:

  • Since the tag is python-3.x, print needs to be called.

  • Need colon after for line.

So the code would look like:

for x in test:
    print(x['start'])
like image 124
U12-Forward Avatar answered Sep 20 '25 20:09

U12-Forward