Thanks Adam, that simplified things a lot. You actually don't need to use JSON to Feature tool. You can save it directly to shapefile. I will put the code below.
However I am still frustrated that I have to go through the service to get to the file instead of doing that in the background. Geoserver has that functionality built in
In any case, if anyone wants to use the script below they will probably want to add a return string with the file name according their needs.
import arcpy
from arcpy import env
import zipfile
import sys
import os
import glob
from random import randint
baseURL= arcpy.GetParameterAsText(0)
where = '1=1'
fields ='*'
token = ''
#The above variables construct the query
query = "?where={}&outFields={}&returnGeometry=true&f=json&token={}".format(where, fields, token)
fsURL = baseURL + query
fs = arcpy.FeatureSet()
fs.load(fsURL)
fs.save(arcpy.env.scratchFolder+"\\"+"download_file.shp")
#Randomize file name
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
#Ziping the shapefile files
def zipShapefile(inShapefile, newZipFN):
if not (os.path.exists(inShapefile)):
print inShapefile + ' Does Not Exist'
return False
if (os.path.exists(newZipFN)):
os.remove(newZipFN)
if (os.path.exists(newZipFN)):
return False
zipobj = zipfile.ZipFile(newZipFN,'w')
for infile in glob.glob( inShapefile.lower().replace(".shp",".*")):
zipobj.write(infile,os.path.basename(infile),zipfile.ZIP_DEFLATED)
zipobj.close()
return True
randomname = str(random_with_N_digits(8))
InputShapeFile = arcpy.env.scratchFolder+"\\"+"download_file.shp"
#Public folder. Probably you will want to change this
OutputZipFile = "C:\\inetpub\\wwwroot\\Shapefiles\\"+randomname+"_file.zip"
zipShapefile(InputShapeFile,OutputZipFile)
print "done!"