Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn this unicode into a dict?

Sorry, I completely neglected to mention that I'm using Python. Let me try this again.

I'm using Python to consume a web service that returns the following JSON:

{
  "results" : [
    {
      "paramName" : "output",
      "dataType" : "GPString",
      "value" : "{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"
    }
  ],
  "messages" : [

  ]
}

Here are the important snippets from my fetching/parsing code:

import urllib
import httplib2
import json
import simplejson
http = httplib2.Http()

headers, response = http.request(url, 'GET')    

if headers['status'] == "200":

    responseAsJson = simplejson.loads(response)

    print "response = " + repr(responseAsJson)
    results = responseAsJson['results'][0]['value']

Unfortunately, this leaves me with the following value for results (as reported in PyScripter Debugger's Variables window):

u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"

I don't know how to then access the address or city keys, for instance.

Can you tell what I'm doing wrong, and how to fix it?

Thanks, Jamie


Old version of my question (obsolete):

Here's the JSON I'm parsing:

response = {u'messages': [], u'results': [{u'dataType': u'GPString', u'value': u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}", u'paramName': u'output'}]}

I've drilled down to this node, and its type is "unicode." How do I make a dict out of this? I think the fact that it's unicode is preventing me from creating the dict or accessing its keys, but I'm not sure.

u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"

Thanks, Jamie

like image 489
Jamie Jackson Avatar asked Dec 06 '25 05:12

Jamie Jackson


1 Answers

Since your results object looks like a string/unicode version of a dict, you need to eval it. A safe way of doing this (as of Python 2.6) would be to use the ast.literal_eval function:

results = ast.literal_eval(responseAsJson['results'][0]['value'])
like image 118
arussell84 Avatar answered Dec 08 '25 20:12

arussell84