<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I get this JSON into a pyhton list or dictionary? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600888#M46949</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Bernie,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use the ast.literal_eval function from the &lt;A href="https://docs.python.org/2/library/ast.html" rel="nofollow noopener noreferrer" target="_blank"&gt;ast &lt;/A&gt;module.&amp;nbsp; Ex:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import urllib
import json
import ast

&lt;SPAN&gt;urlstring = '&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json" rel="nofollow noopener noreferrer" target="_blank"&gt;http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json&lt;/A&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;
ro1 = urllib.urlopen(urlstring)
ro2 = ro1.read()
dict = ast.literal_eval(ro2)
print dict['geometries'][0]['x'], dict['geometries'][0]['y']&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 01:46:12 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-12T01:46:12Z</dc:date>
    <item>
      <title>How do I get this JSON into a pyhton list or dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600886#M46947</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please help.&amp;nbsp; I'm new to Python.&amp;nbsp; I am writing my first python script.&amp;nbsp; 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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;import urllib&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;import json&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;&lt;SPAN&gt;urlstring = '&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json" rel="nofollow" target="_blank"&gt;http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json&lt;/A&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;ro1 = urllib.urlopen(urlstring)&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;ro2 = ro1.read()&lt;/P&gt;&lt;P style="text-align: left; padding-left: 30px;"&gt;print ro2&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P&gt;The response looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {"geometries":[{"x":2488268.7116061845,"y":7667607.8963871095}]}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; The web service response looks like a Python dictionary but When I save it in variable 'ro2' I get a string.&amp;nbsp; How do I read this response into a Python list or dictionary?&amp;nbsp; I need to extract the 'x' and 'y' values.&amp;nbsp; I am working in Python 2.6.5.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the help,&lt;/P&gt;&lt;P&gt;Bernie.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 01:50:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600886#M46947</guid>
      <dc:creator>berniejconnors</dc:creator>
      <dc:date>2015-04-13T01:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get this JSON into a pyhton list or dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600887#M46948</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; ro2 = {"geometries":[{"x":2488268.7116061845,"y":7667607.8963871095}]}
&amp;gt;&amp;gt;&amp;gt; ro2['geometries']
[{'y': 7667607.8963871095, 'x': 2488268.7116061845}]
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; coords = ro2['geometries'][0]
&amp;gt;&amp;gt;&amp;gt; coords['x']
2488268.7116061845
&amp;gt;&amp;gt;&amp;gt; coords['y']
7667607.8963871095
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;to at least give you an idea.&amp;nbsp; I would read up on the json module&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import json
&amp;gt;&amp;gt;&amp;gt; dir(json)
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:46:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600887#M46948</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T01:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get this JSON into a pyhton list or dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600888#M46949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Bernie,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use the ast.literal_eval function from the &lt;A href="https://docs.python.org/2/library/ast.html" rel="nofollow noopener noreferrer" target="_blank"&gt;ast &lt;/A&gt;module.&amp;nbsp; Ex:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import urllib
import json
import ast

&lt;SPAN&gt;urlstring = '&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json" rel="nofollow noopener noreferrer" target="_blank"&gt;http://geonb-t.snb.ca/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?inSR=2219&amp;amp;outSR=2953&amp;amp;geometries=674728.283,5319788.292&amp;amp;transformation=1841&amp;amp;transformForward=TRUE&amp;amp;f=json&lt;/A&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;
ro1 = urllib.urlopen(urlstring)
ro2 = ro1.read()
dict = ast.literal_eval(ro2)
print dict['geometries'][0]['x'], dict['geometries'][0]['y']&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:46:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600888#M46949</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-12T01:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get this JSON into a pyhton list or dictionary?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600889#M46950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jake,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; That looks like the answer I was searching for - Thanks.&amp;nbsp; I'll do some reading on the ast module.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I have also found that this works too:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif;"&gt;&lt;SPAN class="pln" style="color: #000000;"&gt;ro3 = json&lt;/SPAN&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln" style="color: #000000;"&gt;loads&lt;/SPAN&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln" style="color: #000000;"&gt;ro2&lt;/SPAN&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; It appears to be equivalent to:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="pun" style="color: #000000;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #f6f6f6; color: #000000; font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif;"&gt;ro3 = ast.literal_eval(ro2)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Bernie.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 12:45:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-get-this-json-into-a-pyhton-list-or/m-p/600889#M46950</guid>
      <dc:creator>berniejconnors</dc:creator>
      <dc:date>2015-04-13T12:45:36Z</dc:date>
    </item>
  </channel>
</rss>

