0

I need to write out all the variables in this JSON response:

  "result": {
    "\/common\/topic\/weblink": [
      {
        "url": "http:\/\/www.boardgamegeek.com\/boardgame\/13", 
        "description": "BoardGameGeek"
      }
    ], 
    "id": "\/en\/settlers_of_catan"
  }

So to get the id:

result.id

But how do I get the values for "\/common\/topic\/weblink"?

Will Curran
  • 6,959
  • 15
  • 59
  • 92

3 Answers3

2

"\/common\/topic\/weblink" resolves to the string /common/topic/weblink. Any API accessing the decoded content should use the resulting unescaped data; the escaping is just part of JSON's encoding.

This works fine, using the standard JSON module included in Python, which I strongly recommend you use rather than third-party ones unless you have a very strong reason to be different:

import json

json_data = r"""
{
  "result": {
    "\/common\/topic\/weblink": [
      {
        "url": "http:\/\/www.boardgamegeek.com\/boardgame\/13",
        "description": "BoardGameGeek"
      }
    ],
    "id": "\/en\/settlers_of_catan"
  }
}"""

data = json.loads(json_data)
print data["result"]["/common/topic/weblink"]

Note that you left out the enclosing braces on the object, which I added in.

Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
  • Thanks Glenn. I'm on Python 2.5.5 as a limitation of Google App Engine. I believe json was introduced with 2.6. Let me try it out. – Will Curran Dec 19 '10 at 20:48
  • It may be called "simplejson" if it's installed in older versions of Python. (Note that any hosting service which is still on 2.5 in 2010 is clearly not being reasonably maintained, and shouldn't be considered for anything nontrivial.) – Glenn Maynard Dec 19 '10 at 20:57
  • I think I'm close. I imported simplejson. And tried the loads function. >>> json_result = simplejson.loads(result) However I am getting an error (TypeError: expected string or buffer) because the result object is not a string. – Will Curran Dec 19 '10 at 21:05
  • Actually the reason Google App Engine uses 2.5.5 is a good one. And they have the "inventor" of Python on the Google App Engine team. – Will Curran Dec 19 '10 at 21:06
  • @Will: There are no excuses for crippling a platform by using such an obsolete version of Python, and hiring Guido doesn't change that in the slightest--it makes it that much worse (but given the fiasco of Python 3, he's not exactly an authority on software upgrade paths). Anyhow, if your JSON text isn't a string, then I don't know what you're doing--the code I showed above works normally. – Glenn Maynard Dec 19 '10 at 21:59
  • @Will Merydith: If your JSON text is neither `str` nor `unicode`, then what is it? – John Machin Dec 19 '10 at 23:11
  • @John good question. I looked through the Freebase-Python library and it looks like it is also using simplejson.loads, so it should be returning a string. I'm totally confused now. I'm attempting to switch from 10 years of Java to Python this week and it's been interesting ;) – Will Curran Dec 20 '10 at 00:22
  • @John when I attempt to log the JSON result (in order to verify the object type), I get the error that it's a type "attrdict" (http://code.activestate.com/recipes/361668-attrdict-a-dict-whose-items-can-also-be-accessed-a/) – Will Curran Dec 20 '10 at 01:10
0

You want something like

result["\/common\/topic\/weblink"][0].url

(I think)

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • Just for clarity your example has a typo. It should be: result["\/common\/topic\/weblink"][0].url – Will Curran Dec 19 '10 at 20:18
  • This ended up being the correct answer. I was passing the result object to an html template, and for some reason unclear to me, I am having difficulty accessing keys from the JSON result, but there are no issues when accessing them within the Python class. – Will Curran Dec 20 '10 at 01:19
  • When I print the result straight to html I get" {u'/common/topic/weblink': [{u'url': u'http://www.boardgamegeek.com/boardgame/13/Settlers of Catan', u'description': u'BoardGameGeek'}], u'id': u'/en/settlers_of_catan'} – Will Curran Dec 20 '10 at 01:32
0

Try

result.__getattribute__("\/common\/topic\/weblink")[0].url
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144