简体   繁体   English

在Python中从JSON提取特定值

[英]Extracting specific values from JSON in Python

I have a JSON object in Python from the result of a call to an API (using urllib2) generated as follow: 从对API的调用结果(使用urllib2)生成的Python中,我有一个JSON对象,如下所示:

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

This generates a JSON object that contains something similar the following (truncated due to size): 这将生成一个包含以下内容的JSON对象(由于大小而被截断):

"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 the summer of 2010 on HBO's ]Hard Knocks[.\n]",
          "prefix":" New York Jets during the 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 a few training camp glimpses of the ]New York Jets[ during the summer of 2010 on HBO's Hard]",
          "prefix":" Tebow caught a few training camp glimpses of the ",
          "exact":"New York Jets",
          "suffix":" during the summer of 2010 on HBO's Hard",
          "offset":86,
          "length":13
          }
    ],
    "relevance":0.5
}

From this JSON, I would like to extract the "_type" and "name" only if the "typeGroup" == "entities". 从此JSON中,仅当“ typeGroup” ==“ entities”时,我才想提取“ _type”和“ name”。

For example, for the above JSON object the output should look like: 例如,对于上述JSON对象,输出应类似于:

TVShow: Hard Knocks
Organization: New York Jets.

Could someone please help on how to do this in Python? 有人可以帮忙在Python中做到这一点吗?

[UPDATE 1] [更新1]

Based on the answer from Jatin I tried the following: 根据Jatin的回答,我尝试了以下操作:

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

However, this results in the error KeyError: '_typeGroup' 但是,这会导致错误KeyError:'_typeGroup'

I tried to see how the keys and value are printed as follows: 我试图查看键和值的打印方式,如下所示:

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

This resulted in the following output (showing just one 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 the summer of 2010 on HBO's Hard", u'prefix': u' Tebow caught a few training camp glimpses of the ', u'detection': u"[ Tebow caught a few training camp glimpses of the ]New York Jets[ during the 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 to be a nested JSON. 它似乎是嵌套的JSON。 So i tried the following to access the inner Key Value pair as follows: 所以我尝试了以下方法来访问内部键值对,如下所示:

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

However, it throws the following error: 但是,它引发以下错误:

TypeError: string indices must be integers
for key,value in json1.items():
    if value.get('typeGroup') == "entities":
        print value.get('_type'), value.get('name')

Try this and let me know. 试试这个,让我知道。 IT should work. IT应该工作。

I think you are getting that error because some of the values in your JSON don't have a _typeGroup . 我认为您会收到该错误,因为JSON中的某些值没有_typeGroup Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM