<?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: Parse data from JSON service in several txt files in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234158#M18154</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ivan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You are printing a dictionary as a string. You could do something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;flds = ['TipoValore', 'Data', 'Valore']
print ",".join(flds)
for i in precip:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fld in flds:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lst.append(i[fld])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ",".join(lst)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which will yield this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TipoValore,Data,Valore
@,2012-01-01 09:00:00,
@,2012-01-02 09:00:00,
@,2012-01-03 09:00:00,
@,2012-01-04 09:00:00,
...
P,2016-01-03 09:00:00,22.0
P,2016-01-04 09:00:00,3.0
P,2016-01-05 09:00:00,3.2
P,2016-01-06 09:00:00,3.8
P,2016-01-07 09:00:00,3.2&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 11:50:14 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-12-11T11:50:14Z</dc:date>
    <item>
      <title>Parse data from JSON service in several txt files</title>
      <link>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234157#M18153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm new in JSON and Python, I need to parse rainfall data from json hyperlinks that I saved in a shapefile. &lt;/P&gt;&lt;P&gt;This shapefile was created from a JSON service (&lt;A href="http://www.sir.toscana.it/archivio/dati.php?D=json_stations" title="http://www.sir.toscana.it/archivio/dati.php?D=json_stations"&gt;http://www.sir.toscana.it/archivio/dati.php?D=json_stations&lt;/A&gt; ) that contain X, Y coordinates of gauge stations and an hyperlink of rainfall data for each gauge station. This is the result:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Cattura.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/166006_Cattura.PNG" style="width: 620px; height: 281px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now I want to create a script to save data contained in each link in a new text/csv/xls file using CODICE field as name for each output file. Or, if possible, save all the rainfall data (Stored in "SerieDati" --&amp;gt; "Valore") for each station (named "Codice" in json file) in an unique txt file in this format: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-3 jive-image" src="https://community.esri.com/legacyfs/online/166138_pastedImage_13.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;P&gt;This is the model builder:&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-2 jive-image" height="273" src="https://community.esri.com/legacyfs/online/166137_pastedImage_8.png" style="height: 273px; width: 458.974px;" width="459" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but i don't now how to finish my script.This is the script for one station...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;--------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;import arcpy, sys, os, json, urllib2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;urlink = arcpy.GetParameterAsText(0)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;json_string = urllib2.urlopen(urlink).read()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data = json.loads(json_string)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#I want to print results for each station...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sys.stdout = open("C:\\Users\\ivn\\Desktop\\prova.txt", "w")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;precip = data['properties']['SerieDati']&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for i in precip:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt; print i &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#And then...&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;This is the output for one station :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-23 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-24 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-25 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-26 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-27 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-28 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-02-29 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-03-01 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-03-02 09:00:00', u'Valore': u'28.8'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-03-03 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-03-04 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;{u'TipoValore': u'V', u'Data': u'2000-03-05 09:00:00', u'Valore': u'0.0'}&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How to remove {,},u,' characters from output?&lt;/P&gt;&lt;P&gt;And this is JSON data for one station:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sir.toscana.it/archivio/dati.php?IDST=pluvio&amp;amp;D=json&amp;amp;IDS=TOS19000114" title="http://www.sir.toscana.it/archivio/dati.php?IDST=pluvio&amp;amp;D=json&amp;amp;IDS=TOS19000114"&gt;http://www.sir.toscana.it/archivio/dati.php?IDST=pluvio&amp;amp;D=json&amp;amp;IDS=TOS19000114&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;{"type":"Feature","geometry":{"type":"Point","coordinates":[11.6617,42.8829]},"properties":{&lt;SPAN style="color: #3d3d3d;"&gt;&lt;STRONG&gt;"Codice":"TOS19000114"&lt;/STRONG&gt;,&lt;/SPAN&gt;"Nome":"Abbadia S. S. - Laghetto Verde","Comune":"Abbadia S.S.","Provincia":"SI","Quota":"910.00 mslm","UnitaMisura":"mm","TipiValore":{"N":"Non Validato","P":"Prevalidato","V":"Validato","@":"Mancante","R":"Ricostruito"},"TipoDati":"PLUVIOMETRIA - Aggregazione a 24 ore (9-9)",&lt;STRONG&gt;"SerieDati"&lt;/STRONG&gt;:[{"Data":"2012-01-01 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-02 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-03 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-04 09:00:00","Valore":"","TipoValore":"@"},{&lt;STRONG&gt;"Data":"2012-01-05&lt;/STRONG&gt; 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-06 09:00:00",&lt;STRONG&gt;"Valore":""&lt;/STRONG&gt;,"TipoValore":"@"},{"Data":"2012-01-07 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-08 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-09 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-10 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-11 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-12 09:00:00","Valore":"","TipoValore":"@"},{"Data":"2012-01-13&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;THANKS! &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Jan 2016 12:19:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234157#M18153</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2016-01-08T12:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Parse data from JSON service in several txt files</title>
      <link>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234158#M18154</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ivan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You are printing a dictionary as a string. You could do something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;flds = ['TipoValore', 'Data', 'Valore']
print ",".join(flds)
for i in precip:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fld in flds:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lst.append(i[fld])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ",".join(lst)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which will yield this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TipoValore,Data,Valore
@,2012-01-01 09:00:00,
@,2012-01-02 09:00:00,
@,2012-01-03 09:00:00,
@,2012-01-04 09:00:00,
...
P,2016-01-03 09:00:00,22.0
P,2016-01-04 09:00:00,3.0
P,2016-01-05 09:00:00,3.2
P,2016-01-06 09:00:00,3.8
P,2016-01-07 09:00:00,3.2&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:50:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234158#M18154</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T11:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: Parse data from JSON service in several txt files</title>
      <link>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234159#M18155</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Xander,&lt;/P&gt;&lt;P&gt;I solved my problem in this inelegant way: &lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" height="349" src="https://community.esri.com/legacyfs/online/166929_pastedImage_2.png" style="width: 492px; height: 349.042px;" width="492" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the final model builder, that save rainfall data for each gauge station in a txt file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-2 jive-image" height="302" src="https://community.esri.com/legacyfs/online/166930_pastedImage_3.png" style="height: 302px; width: 347.332px;" width="347" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the output txt file:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-3 jive-image" height="344" src="https://community.esri.com/legacyfs/online/166931_pastedImage_4.png" style="height: 344px; width: 264.895px;" width="265" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I merged all txt files and I used "Make query table" to join rainfall data to gauge station shapefile using as key the field StazID. The query: met.StazID = staz.CODICE &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-4" height="259" src="https://community.esri.com/legacyfs/online/166933_pastedImage_6.png" style="width: 442px; height: 259.497px;" width="442" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; And this is an example of interpolated map of cumulative rainfall for year 2003 (using kriging):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-5 jive-image" height="252" src="https://community.esri.com/legacyfs/online/166934_pastedImage_8.png" style="width: 252px; height: 252px;" width="252" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Jan 2016 09:51:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234159#M18155</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2016-01-10T09:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: Parse data from JSON service in several txt files</title>
      <link>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234160#M18156</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ivan, I'm glad you resolved it, but I would strongly suggest using the key (fieldname) to retrieve a value from a dictionary over slicing it as text. Slicing text will require that the data always has the same length. Which might be the case in your project, but often it is not the case.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Jan 2016 16:03:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-data-from-json-service-in-several-txt-files/m-p/234160#M18156</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2016-01-10T16:03:38Z</dc:date>
    </item>
  </channel>
</rss>

