0

I have a JSON object in Python from the result of a call to an API (using urllib2) generated as follow:

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

This generates a JSON object that contains something similar the following (truncated due to 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 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".

For example, for the above JSON object the output should look like:

TVShow: Hard Knocks
Organization: New York Jets.

Could someone please help on how to do this in Python?

[UPDATE 1]

Based on the answer from Jatin I tried the following:

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

However, this results in the error 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. 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
4
  • A json-object in Python is just another dictionary. Do you know how to access items of a dictionary? Commented Apr 5, 2015 at 10:48
  • I am a basic user of Python. I could access it by using json1["d.opencalais.com/genericHasher-1/…. However, I would like to do it by looping it through each key and check for conditions nested keys. Not sure how to do it. Commented Apr 5, 2015 at 10:53
  • 1
    I'm merely trying to improve your problem-tackling skills. So you know you want to use a loop to iterate over the keys. Python dictionaries provide methods to obtain all the keys, so that you don't have to type them manually. You could check out the documentation on dict.keys() or even more simply for key in dict:. Commented Apr 5, 2015 at 10:57
  • I have tried following Jatin's lead to solve it but still facing some issues. I have updated the OP. Please let me know where I am going wrong with this Commented Apr 5, 2015 at 12:31

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

This throws an error: KeyError: '_typeGroup'. I have updated my original post with more information
1

I think you are getting that error because some of the values in your JSON don't have a _typeGroup. Try this:

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

3 Comments

This worked fine. Could you please explain what value.get("_typeGroup", "") actually does?
@Ravi it takes an element from the dictionary corresponding with the key _typeGroup, IF it exists, otherwise it will take the default value, which is given by the 2nd positional argument "", the empty string here. I do think jatin's answer lead you there, so you should consider upvoting his answer as well as he could not have known not all values were present.
Sure, the get method allows you to return a default value if the key you're looking for is not found in the dictionary (documentation). I also just found out that it returns None if no default is specified, so the above could have just been value.get("_typeGroup")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.