Extracting specific values from JSON in Python -



Extracting specific values from JSON in Python -

i have json object in python result of phone call api (using urllib2) generated follow:

results = urllib2.urlopen(req).read() json1 = json.loads(results)

this generates json object contains similar next (truncated due size):

"http://d.opencalais.com/dochash-1/895ba8ff-4c32-3ae1-9615-9a9a9a1bcb39/cat/1":{ "_typegroup":"topics", "category":"http://d.opencalais.com/cat/calais/entertainment_culture", "classifiername":"calais", "categoryname":"entertainment_culture", "score":1 }, "http://d.opencalais.com/generichasher-1/b6a2d07d-133b-35ad-85e2-54d524e750cf":{ "_typegroup":"entities", "_type":"tvshow", "name":"hard knocks", "_typereference":"http://s.opencalais.com/1/type/em/e/tvshow", "instances":[ { "detection":"[ new york jets during summer of 2010 on hbo's ]hard knocks[.\n]", "prefix":" new york jets during summer of 2010 on hbo's ", "exact":"hard knocks", "suffix":".\n", "offset":135, "length":11 } ], "relevance":0.5 }, "http://d.opencalais.com/generichasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3":{ "_typegroup":"entities", "_type":"organization", "name":"new york jets", "organizationtype":"sports", "nationality":"american", "_typereference":"http://s.opencalais.com/1/type/em/e/organization", "instances":[ { "detection":"[ tebow caught few training camp glimpses of ]new york jets[ during summer of 2010 on hbo's hard]", "prefix":" tebow caught few training camp glimpses of ", "exact":"new york jets", "suffix":" during summer of 2010 on hbo's hard", "offset":86, "length":13 } ], "relevance":0.5 }

from json, extract "_type" , "name" if "typegroup" == "entities".

for example, above json object output should like:

tvshow: hard knocks organization: new york jets.

could please help on how in python?

[update 1]

based on reply jatin tried following:

for key,value in json1.items(): if value["_typegroup"] == "entities": print value['_type'], value['name']

however, results in error keyerror: '_typegroup'

i tried see how keys , value printed follows:

for key,value in json1.items(): print key,value

this resulted in next output (showing 1 key, value pair):

http://d.opencalais.com/generichasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3 {u'_typereference': u'http://s.opencalais.com/1/type/em/e/organization', u'_type': u'organization', u'name': u'new york jets', u'_typegroup': u'entities', u'instances': [{u'suffix': u" during summer of 2010 on hbo's hard", u'prefix': u' tebow caught few training camp glimpses of ', u'detection': u"[ tebow caught few training camp glimpses of ]new york jets[ during summer of 2010 on hbo's hard]", u'length': 13, u'offset': 86, u'exact': u'new york jets'}], u'relevance': 0.5, u'nationality': u'american', u'organizationtype': u'sports'}

it appears nested json. tried next access inner key value pair follows:

for key,value in json1.items(): val1 = value key,value in val1.items(): if value["_typegroup"] == "entities": print value['_type'], value['name']

however, throws next error:

typeerror: string indices must integers

i think getting error because of values in json don't have _typegroup. seek this:

for key,value in x.items(): if value.get("_typegroup", "") == "entities": print value['_type'], value['name']

python json urllib2

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -