4

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
  • 9,280
  • 9
  • 43
  • 57
Tony Scialo
  • 5,457
  • 11
  • 35
  • 52

3 Answers3

5

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'])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

worked for me!

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

OP:

ieo5
qvc5
ieo5
Amitkumar Karnik
  • 912
  • 13
  • 23
1

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

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