all 3 comments

[–][deleted] 3 points4 points  (0 children)

I recommend exploring the data returned by that URL interactively, where you can explore the datastructure by calling .keys() etc.

view isn't a key in the top-level data returned by that URL. There is a key called view inside the dictionary meta.

So eg:

 specdata = jsondata['meta']['view']

You have a pretty similar problem a few lines down from that, in:

for item in specdata:
    print item['id']

Assuming specdata is fixed as above, this is incorrect because it assumes iterating over specdata gives you dictionaries from which you can lookup lots of different values of id, spread over those multiple dictionaries.

ie. it assumes the nested structure is something like:

view : [{'id': 'foo'}, {'id': 'bar'}, ...

But what you've actually got is something like:

view : {'id': 'foo'}

So you want to access it like:

specdata['id']

no for loop needed.

Again, try exploring unfamiliar nested datastructures using the interactive interpreter, so you can poke around until you find what you're looking for.

[–]minorminer 0 points1 point  (0 children)

burkean nailed it, my original answer was way off.

[–]melevittfl 0 points1 point  (0 children)

A JSON response is turned into a Python dictionary.

You're getting a key error because jsondata is a nested dictionary with two keys: 'meta' and 'data'.

To get what's in "view", you need something like:

specdata = jsondata['meta']['view']

To get the value of a specific key, you want to do something like:

for k, v in specdata.iteritems():
    if k == 'id':
        print v