Error parsing JSON in python -


i'm trying parse reddit json file titles of posts, when use code:

import urllib2 import json  response = urllib2.urlopen('http://www.reddit.com/r/technology/.json') data = json.load(response)    print data['title'] 

i error:

traceback (most recent call last):   file "c:\users\harry\desktop\py\jason.py", line 6, in <module>     print data['title'] keyerror: 'title' 

any appreciated, thanks.

that response has no 'title' top-level key. 'kind' , 'data' keys found in top-level dictionary.

you need more closely @ structure here; data key contains actual payload, that's dictionary without 'title' key well.

here elided version of contents:

>>> import pprint >>> pprint.pprint(data) {u'data': {u'after': u't3_1wnfuv',            u'before': none,            u'children': [{u'data': {u'approved_by': none,                                     u'author': u'papa00king',                                     # [...]                                     # many keys omitted                                     # [...]                                     u'title': u'uk government unblock websites after adult filter blunder',                                     u'ups': 1030,                                     u'url': u'http://www.engadget.com/2014/01/31/uk-government-adult-filter-whitelist/',                                     u'visited': false},                           u'kind': u't3'},                           # [...]                           # many many more children                           # [...]                           ],            u'modhash': u''},  u'kind': u'listing'} 

if looking titles of children in result, use:

for child in data['data']['children']:     print child['data']['title'] 

in other words, each 'object' has kind , data keys, , listing kind of object has children key, list of more objects kind , data keys. t3 kind of objects looking have title keys in data structure.

demo:

>>> child in data['data']['children']: ...     print child['data']['title'] ...  uk government unblock websites after adult filter blunder upside being let go nokia - thousands of workers made redundant have set on 400 new technology companies using grants of €20,000 - costing nokia 'tens of millions of euro's' wouldn't download car: honda releases concept car 3d printing files 3d printing provide new solutions disabled people? - disabled people need personalised equipment individual needs. 3d printing provide cheap , efficient solution? 5 of india's popular small cars fail first ever crash tests, showing lead fatalities or serious injuries if involved in accident. kansas legislature wants stop other kansas cities getting google fiber google’s latest moves toward scarily personalized searches $75 synthesizer turn instrument best buy slashes price of surface pro $400 computer sees when watches matrix windows 8.1 update hide tile interface default young farmers win award designing cattle management app ibm's speedy graphene chip lead super-efficient mobile devices south korea fines google $196k illegal data gathering microsoft said preparing promote within, make satya nadella next ceo pirate bay ban lifted in netherlands blocking torrent sites ruled ‘ineffective’ haptics, trackballs , cheeto dust: how valve built game controller of future icelandic drilling project opens door volcano-powered electricity here's how know if gmail account has been hacked amazon considering $20 $40 price hike prime service in massive spending on computing gear underpins google profits yahoo mail accounts breached, number of affected users not disclosed amd (almost) rolls out mantle, high performance alternative direct3d , opengl physicists create synthetic magnetic monopole predicted more 80 years ago facebook let go anonymous first time 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -