pythonjsonarcgis-server

Create a json object in python with nested dictionary and list


I am trying to create in python the following payload in order to make a POST request. The following line comes from the REST client in my browser and creates the result I want, and I know it works.

(URLencoded form data) features=[{"attributes":{"POINT_NAME":"tpoint","EASTING":"338250","NORTHING":"342300","CREATION_DATE":"16/12/2020",},"geometry":{"x":338250,"y":342300}}]&f=json

So far, I did a number of attempts with the closest being the following:

import json
import requests

#Create inner dictionary 
data_dictionary = {}
data_dictionary['attributes'] = {"POINT_NAME" : "tpoint", "EASTING" : "338250", "NORTHING" : "342300", "CREATION_DATE" : "16/12/2020"}
data_dictionary['geometry'] ={"x" : 338250, "y" : 342300}
data_list = [data_dictionary]
payload = {"features": data_list , "f" : "json"}

r = request.post(API_ENDPOINT_POINT, data = payload) 
r.text

payload output is: {'features': [{'attributes': {'POINT_NAME': 'tpoint', 'EASTING': '338250', 'NORTHING': '342300','CREATION_DATE': '16/12/2020'},'geometry': {'x': 338250, 'y': 342300}}], 'f': 'json'}, but when I make the request I get: '{"error":{"code":500,"message":"Unable to complete operation.","details":["Parser error: Some parameters could not be recognized."]}}'

Any ideas would be helpful, I am stuck.

Thanks


Solution

  • Copying the answer from comments: try to use payload as

    payload = {"features": json.dumps(data_list) , "f" : "json"}