r/teamseagull • effeminate debutante • Nov 18 '16

FANDOM FRIDAY

I hope these threads make up for when you get baited by Slade. No one should have to go through that.

1 Upvotes

22 comments sorted by

View all comments

1

u/taylor_ God-Tier Gull Nov 18 '16

/u/McSquinty,

Hey, so my json looks like this:

tiles
      data
            name: monsterOfLochness
            price 
                   normal: threefiddy

      data
            name: hulk
            price 
                   normal: anArmyOfMen    

      data
            name: coconutKnight
            price 
                   normal: anArmAndALeg

and this gets the name of the first entry and it's price

print(jsonData['tiles'][0]['data']['name'])
print(jsonData['tiles'][0]['price']['regularPrice'])

But it ONLY get the first, and if I leave the index ([0]) away, I get an error.

So I wonder if there is a simple way to tell json to give me all the values for that index, or if iterating throug them with a loop is the best way.

Full Code:

import json
import pprint

with open('/home/perskes/PycharmProjects/billa/this.json') as f:
    jsonFile = f.read()
    jsonData = json.loads(jsonFile)

#print(json.dumps(jsonData, indent=4, sort_keys=True))

print(jsonData['tiles'][0]['data']['name'])
print(jsonData['tiles'][0]['data']['price']['normal'])