JSwordy JSwordy - 2 years ago 71
JSON Question

Extracting Data from JSON

I have a large JSON item returned through a REST API, I wont junk up this with the full text but here is the code I am currently using:

import urllib2
import json
req = urllib2.Request
response = urllib2.urlopen(req)
the_page = response.read()
decode = json.loads(the_page)
#print = decode #removed, because it is not actually related to the question
print decode

I have been trying to extract information out of it such as the date polls are updated, the actual data from the polls etc (particularly the presidential polls) but I am having trouble returning any data at all. Can anyone assist?

The actual question is how to query data from the returned array/dict

Answer Source

The problem is, that you overwrite print with your data, instead of printing the data. Just remove the = in the last line and it should work fine:

print decode

If you want to use Python 3, you need parenthesis for print. This would look like this:


Edit: As you updated your question, here an answer to your actual question: The data is returned as a combination of dicts and lists by the loads function. Hence you can also access the data like a dict/list. For example, to get the last_updated field of all polls in one list, you can do something like this:

all_last_updated = [poll['last_updated'] for poll in decode]

Or to just get the end date of all polls sponsored by "Constitutional Responsibility Project", you could do this:

end_dates = [poll['end_date'] for poll in decode if any(sponsor['name'] == 'Constitutional Responsibility Project' for sponsor in poll['sponsors'])

Or if you just want the id of the first poll in the list, do:

the_id = decode[0]['id']

You access anything you want from the json in a similar way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download