The first thing I would do when working with a big lump of json, is try to get it into a more readable format. This online json formatting tool should do the job.
Here's some code that will get all the trend names:
import urllib2
import json
url = 'http://api.twitter.com/1/trends/44418.json'
# download the json string
json_string = urllib2.urlopen(url).read()
# de-serialize the string so that we can work with it
the_data = json.loads(json_string)
# get the list of trends
trends = the_data[0]['trends']
# print the name of each trend
for trend in trends:
print trend['name']
Or you can do it all in one line:
names = [trend['name'] for trend in the_data[0]['trends']]
for name in names:
print name
Both will result in:
#throwagrenade
Rebecca Black
Eric Abidal
#questionsihate
#juniordoctors
Smiley Culture
Lily Allen
Wes Brown
Pandev
Ray Wilkins
Relevant reading:
Python docs on json (although you should only really need json.loads()
)
Dive Into Python's sections on lists and dictionaries.