JSON as toolbox parameter

232
2
Jump to solution
03-28-2020 01:34 PM
jaykapalczynski
Frequent Contributor

I am trying to pass some record data from Javascript to GP Service which accepts one parameter

I want to grab this parameter on the python side and parse it to variables.

I am missing something here.....Having issues....

I can pass a string to the python script so that part works....

I am just unsure of I have to JSON string formatted properly before I pass it from Javascript

or if I am trying to parse it incorrectly on the python side.....these are the steps I am taking...

Thoughts?

Step 1

Basically I am confused on how to create the JSON file on the javascript side....This will hold 3 records

I can format string to look like this and then set to JSON

var dict =  [  {'Results':
   ['address': 'some address', 'id': 'sone ID' , 'distance': 'some distance'],
   ['address': 'some address', 'id': 'sone ID' , 'distance': 'some distance'],
   ['address': 'some address', 'id': 'sone ID' , 'distance': 'some distance']
} ]

var dictstring = JSON.stringify(dict);

gp.execute(dictstring );‍‍‍‍‍‍‍‍‍

Step 2

I then need to pass this to a GP service I set up with an input parameter called '"Request"

Question:

When setting up the parameters do I select "STRING" ???

Step 3

import arcpy
import json

jsondata = arcpy.GetParameterAsText(0)‍‍‍‍

for each in jsondata[Results][0]: ‍‍‍‍‍‍
   varId = (each['address'])
   varAddress = (each['id'])
   varDistance = (each['distance'])‍‍‍‍‍‍‍‍‍

   # do something with each of these record sets.‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
jaykapalczynski
Frequent Contributor

solved

I was not setting up the params variable properly

nor was I handling the parameter in Python properly...

This works for me....any questions hit me up

var dictstring = '{"employees": [{ "address": "1520 Split Oak Ln, Henrico, Virginia, 23229", "distance": "10", "id": "1" }, { "address": "113 Buffalo Rd, Clarksville, Virginia(VA), 23927", "distance": "50", "id": "4" }, { "address": "8817 Sherando Dr, Bristow, Virginia(VA), 20136", "distance": "20", "id": "5" }]}';

var params = {
     request: dictstring
};
gpJSON.execute(params);

import arcpy
import requests
import json

dataInputParameter1 = arcpy.GetParameterAsText(0)

textDict = json.loads(dataInputParameter1)

for employee in textDict['employees']:
    varsearchAddress =  employee["address"]
    varsearchDistance =  employee["distance"]
    varsearchid =  employee["id"]

    print ("address: " + varsearchAddress)
    print ("distance: " + varsearchDistance)
    print ("uniqueid: " + varsearchid)

View solution in original post

0 Kudos
2 Replies
jaykapalczynski
Frequent Contributor

solved

I was not setting up the params variable properly

nor was I handling the parameter in Python properly...

This works for me....any questions hit me up

var dictstring = '{"employees": [{ "address": "1520 Split Oak Ln, Henrico, Virginia, 23229", "distance": "10", "id": "1" }, { "address": "113 Buffalo Rd, Clarksville, Virginia(VA), 23927", "distance": "50", "id": "4" }, { "address": "8817 Sherando Dr, Bristow, Virginia(VA), 20136", "distance": "20", "id": "5" }]}';

var params = {
     request: dictstring
};
gpJSON.execute(params);

import arcpy
import requests
import json

dataInputParameter1 = arcpy.GetParameterAsText(0)

textDict = json.loads(dataInputParameter1)

for employee in textDict['employees']:
    varsearchAddress =  employee["address"]
    varsearchDistance =  employee["distance"]
    varsearchid =  employee["id"]

    print ("address: " + varsearchAddress)
    print ("distance: " + varsearchDistance)
    print ("uniqueid: " + varsearchid)
0 Kudos
jslatane
New Contributor

For anyone else that comes across this and is trying to decipher the code blocks in @jaykapalczynski 's post (I don't know Javascript so take that one for what it's worth):

 

var dictstring = '{"employees": [{ "address": "1520 Split Oak Ln, Henrico, Virginia, 23229", "distance": "10", "id": "1" }, { "address": "113 Buffalo Rd, Clarksville, Virginia(VA), 23927", "distance": "50", "id": "4" }, { "address": "8817 Sherando Dr, Bristow, Virginia(VA), 20136", "distance": "20", "id": "5" }]}';
var params = {request: dictstring};
gpJSON.execute(params);‍‍‍‍‍‍
import arcpy
import requests
import json
dataInputParameter1 = arcpy.GetParameterAsText(0)
textDict = json.loads(dataInputParameter1)
for employee in textDict['employees']:
    varsearchAddress = employee["address"]
    varsearchDistance = employee["distance"]
    varsearchid =  employee["id"]
    print ("address: " + varsearchAddress)
    print ("distance: " + varsearchDistance)
    print ("uniqueid: " + varsearchid)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

0 Kudos