Hedde van der Heide
Hedde van der Heide

Reputation: 22459

JSON return to python iterable

I am trying to get this return:

Google API

Into a Python iterable object

So far I have this:

request = "http://maps.googleapis.com/maps/api/geocode/%s?address=%s&sensor=%s" % (self._output, self._address, self._sensor)
data = urllib.urlopen(request).read()
decoded_data = json.loads('[%s]' % data).pop()
    if decoded_data.get("status") == "OK":
        return decoded_data
    return ""

However this only turns the outer "wrapper" into a dictionary. I want to decode the entire block so I can access the values easily and raise exceptions where needed.

Upvotes: 0

Views: 487

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

Why are you wrapping it in a list? It's a JSON object, which maps very cleanly to a Python dictionary. Just do decoded_data = json.loads(data), then you can do decoded_data['results'][0]['formatted_address'] etc.

Upvotes: 3

Related Questions