AnsweredAssumed Answered

Looping through ArcGIS JSON Service and writing results to CSV

Question asked by geoffreywestgis on Feb 5, 2015
Latest reply on Feb 5, 2015 by geoffreywestgis

I have a sample script that I am using as a proof of concept to loop through a JSON web-service and write the results to a CSV then table in file geodatabase. I am using my The ArcGIS Sample Server JSON web-service as an example.  The problem is that my script does not write all records to the CSV, however in this case only the last record.



import requests
import json
import urllib2
import csv
import arcpy

url = ""
parsed_json = json.load(urllib2.urlopen(""))
details = {'layers': 'name'}
headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': '/'}
response =, data=json.dumps(details), headers=headers)

# tell computer where to put CSV

# open it up, the w means we will write to it
writer = csv.writer(open(outfile_path, 'wb'))

#create a list with headings for our columns
headers = ['name', 'id']

#write the row of headings to our CSV file

for services in parsed_json ["layers"]:

   #initialize the row
   row = []
   #add every 'cell' to the row list, identifying the item just like an index in a list


   #once you have all the cells in there, write the row to your csv
   #increment our loop counter, now we're on the next time through the loop

i = 1
i = i +1

f = open(outfile_path, 'wb')
writer = csv.writer(f)


#Temporary Delete Function to Overwrite Table
if arcpy.Exists("C:\dev_folder\orginalDev.gdb\jsoncsv7"):

arcpy.TableToTable_conversion("C:\Users\Administrator\Desktop\json2gdb.csv", "C:\dev_folder\orginalDev.gdb", "jsoncsv7")

print response.text



my output is:






I would like to write all layer IDs and Names, not just the last one.