2

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'] 
petezurich
  • 7,683
  • 8
  • 34
  • 51
Tony Scialo
  • 4,697
  • 9
  • 34
  • 50

3 Answers3

3

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

  • Since the tag is , print needs to be called.

  • Need colon after for line.

So the code would look like:

for x in test:
    print(x['start'])
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

worked for me!

for d in test:
     print d['start']

OP:

ieo5
qvc5
ieo5
Amitkumar Karnik
  • 907
  • 14
  • 20
1

The syntax is right just add a colon after the for statement

for x in test:
    print(x['start'])
Knl_Kolhe
  • 163
  • 8