How do I get this JSON into a pyhton list or dictionary?

4574
3
Jump to solution
04-12-2015 06:50 PM
berniejconnors
Occasional Contributor III

Please help.  I'm new to Python.  I am writing my first python script.  I have made good progress but I am having trouble handling the response from a the ArcGIS Server 10.2 REST API 'project' operation. Here is some code that will get a sample response:

import urllib

import json

urlstring = 'http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&outSR...'

ro1 = urllib.urlopen(urlstring)

ro2 = ro1.read()

print ro2

The response looks like this:

    {"geometries":[{"x":2488268.7116061845,"y":7667607.8963871095}]}

   The web service response looks like a Python dictionary but When I save it in variable 'ro2' I get a string.  How do I read this response into a Python list or dictionary?  I need to extract the 'x' and 'y' values.  I am working in Python 2.6.5.

Thanks for the help,

Bernie.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Bernie,

You can use the ast.literal_eval function from the ast module.  Ex:

import urllib
import json
import ast

urlstring = 'http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&outSR...'
ro1 = urllib.urlopen(urlstring)
ro2 = ro1.read()
dict = ast.literal_eval(ro2)
print dict['geometries'][0]['x'], dict['geometries'][0]['y']

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

There is probably a way to read it directly, but I don't work with JSON, the structure appears to be a nested dictionary structure....

>>> ro2 = {"geometries":[{"x":2488268.7116061845,"y":7667607.8963871095}]}
>>> ro2['geometries']
[{'y': 7667607.8963871095, 'x': 2488268.7116061845}]
>>>
>>> coords = ro2['geometries'][0]
>>> coords['x']
2488268.7116061845
>>> coords['y']
7667607.8963871095
>>>

to at least give you an idea.  I would read up on the json module

>>> import json
>>> dir(json)
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
>>>
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Bernie,

You can use the ast.literal_eval function from the ast module.  Ex:

import urllib
import json
import ast

urlstring = 'http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&outSR...'
ro1 = urllib.urlopen(urlstring)
ro2 = ro1.read()
dict = ast.literal_eval(ro2)
print dict['geometries'][0]['x'], dict['geometries'][0]['y']
berniejconnors
Occasional Contributor III

Jake,

     That looks like the answer I was searching for - Thanks.  I'll do some reading on the ast module.

     I have also found that this works too:

ro3 = json.loads(ro2)

     It appears to be equivalent to:

ro3 = ast.literal_eval(ro2)

Bernie.

0 Kudos