To sort the list of dictionaries, use the methodcaller with the key to sort; You want to sort the list of results, not the contained dictionaries. Moreover, some records do not have a year, and this can lead to errors:
from operator import methodcaller for row in sorted(json_object['results'], key=methodcaller('get', 'year', None)):
The definition of methodcaller will basically do entry.get('year', None) for each entry in json_object['results'] , providing the sorted method sorted value for sorting.
You should not use readlines() to read your JSON response, this will interpret newlines incorrectly. Let the json library read it instead (note the .load() , no s at the end):
response = urllib2.urlopen(request) json_object = json.load(response)
source share