Friday, 14 June 2019

How to prepare JSON payload for ArcGIS Feature Service using Python

There you have it - this is just a dump from my code - purely to remind myself...

What it does it creates a point array then using Python it puts this payload to an ArcGIS Feature Service.


ptemp = {}
... print type(ptemp)
... ptemp["attributes"]={}
... ptemp["attributes"]["objectid"]=1
... ptemp["attributes"]["s"]=223
... ptemp["attributes"]["a"]="a val"
... ptemp["attributes"]["n"]="n val"
... ptemp["attributes"]["b"]=123
... ptemp["attributes"]["p"]="p val"
... ptemp["attributes"]["g1"]=11
... ptemp["attributes"]["g2"]=22
... ptemp["attributes"]["g3"]=33
... ptemp["attributes"]["g4"]=44
... ptemp["attributes"]["g5"]=55
... ptemp["geometry"]={}
... ptemp["geometry"]["x"]=152.95359856272353
... ptemp["geometry"]["y"]=-23.461733290059232
... ptemp["geometry"]["spatialReference"]={"wkid" : 4326}

So we created the ptemp dictionary object with point parameters.

>>> url = config["serviceurl"]+"/0/addFeatures?token={}&f=pjson"
... token = getToken("uname", "password")
... url = url.format(token)

Now we acquired the token and added to url.

>>> pptemp = []
>>> pptemp.append(ptemp)

Now we just put the dictionary to a list - this needed for the ArcGIS Feature Service.

pt = urllib.quote_plus(json.dumps(pptemp))

Now we created an encoded payload from json string.
st='features='+pt
st=st+'&gdbVersion=&rollbackOnFailure=true&f=pjson'
...and finally we send all of these to the ArcGIS Feature Service in a POST request:

>>> request.add_header("Content-Type",'application/x-www-form-urlencoded')
>>> request = urllib2.Request(url, data=st)
>>> connection = opener.open(request)
>>> connection.readlines()

No comments:

Post a Comment