parse JSON string

94
7
Jump to solution
03-30-2020 11:39 AM
jaykapalczynski
Frequent Contributor

I am passing the below from Javascript as a parameter string via a GP Service.

Once I grab this in the python script I am trying to parse it....

I am trying to write variables for address, distance, and id for each record in a for loop....but having issues..thoughts?

for item in textstring['employees']:
varaddress = item["address"]
print "address: " + varaddress

var text = '{"employees":[' +
'{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },' +
'{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },' +
'{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },' +
']} ';
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hey Jay,

Use json.loads to convert the JSON string to a Python Dictionary. 

import json

text = '{"employees":[{"address":"113 Buffalo Rd, Clarksville, VA 23927","distance":"50","id" : "4"},{"address":"8817 Sherando Dr, Bristow, VA 20136","distance":"20","id" : "5" }]}'

textDict = json.loads(text)

for employee in textDict['employees']:
varaddress = employee["address"]
print(varaddress)

View solution in original post

7 Replies
jaykapalczynski
Frequent Contributor

In the original post the first part was in Python the second was in Javascript....that is what I am trying to pass as a parameter to the service and eventually the python scripts

Here is an example of a shortened string

'{"employees":[{"address":"113 Buffalo Rd, Clarksville, VA 23927","distance":"50","id" : "4"},{"address":"8817 Sherando Dr, Bristow, VA 20136","distance":"20","id" : "5" }]} '
‍‍
0 Kudos
AaronPulver
Esri Regular Contributor

Are you just trying to parse that string into a dictionary? Use json.loads

import json
data = json.loads(text)

print(data["employees"])

0 Kudos
jaykapalczynski
Frequent Contributor

I can get my string to this in python but need to loop through and set each of the items to variables

DATA: '{"employees":[{"address":"113 Buffalo Rd, Clarksville, VA 23927","distance":"50","id" : "4"},{"address":"8817 Sherando Dr, Bristow, VA 20136","distance":"20","id" : "5" }]} '

JSON: "{\"employees\":[{\"address\":\"113 Buffalo Rd, Clarksville, VA 23927\",\"distance\":\"50\",\"id\" : \"4\"},{\"address\":\"8817 Sherando Dr, Bristow, VA 20136\",\"distance\":\"20\",\"id\" : \"5\" }]} "

ENCODED: "{\"employees\":[{\"address\":\"113 Buffalo Rd, Clarksville, VA 23927\",\"distance\":\"50\",\"id\" : \"4\"},{\"address\":\"8817 Sherando Dr, Bristow, VA 20136\",\"distance\":\"20\",\"id\" : \"5\" }]} "

DECODED: {"employees":[{"address":"113 Buffalo Rd, Clarksville, VA 23927","distance":"50","id" : "4"},{"address":"8817 Sherando Dr, Bristow, VA 20136","distance":"20","id" : "5" }]}
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hey Jay,

Use json.loads to convert the JSON string to a Python Dictionary. 

import json

text = '{"employees":[{"address":"113 Buffalo Rd, Clarksville, VA 23927","distance":"50","id" : "4"},{"address":"8817 Sherando Dr, Bristow, VA 20136","distance":"20","id" : "5" }]}'

textDict = json.loads(text)

for employee in textDict['employees']:
varaddress = employee["address"]
print(varaddress)

View solution in original post

jaykapalczynski
Frequent Contributor

One more thing....I am very confused on

I can get this to work from running the python script seen below using the dataInputParameter variable

ALTHOUGH I am trying to run this from a ESRI tool that passes in a parameter 

If I change the below code to accept the dataInputParameter1 variable which is the STRING being passed to it from the ESRI Tool it blows up

textDict = json.loads(dataInputParameter1)

So I change to the code above

Copy the text from the dataInputParameter variable seen below

Launch the tool and paste that into the parameter and submit it blows up.

BUT as you can see I have a second parameter that returns results....I am returning all 3 Variables seen below...and the ONLY difference is that when I return the 3rd variable (the one that works when I define it) is it missing the single ' on the beginning and end....

QUESTION:  do I need to strip the variable that I am grabbing from the GetParametersAsText?????

Why does it work with a manual variable and not the parameter text?  

MESSAGE Variable 1:  is the  dataInputParameter1 variable that is created from the string being passed for the Tool 
MESSAGE Variable 2:  is the dataInputParameter2 variable simply set form the getParameterAsText variable
MESSAGE Variable 3: is the variable that I create with the text seen below...this is ALSO the text that I copy into the string parameter in the ESRI Tool

dataInputParameter1 = arcpy.GetParameterAsText(0)

dataInputParameter2 = dataInputParameter1

dataInputParameter = '{"employees":[{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"113 Buffalo Rd, Clarksville, Virginia(VA), 23927","distance":"50", "id" : "4" },{"address":"8817 Sherando Dr, Bristow, Virginia(VA), 20136","distance":"20", "id" : "5" }]}'


varaddress = ""
vardistance = ""
varid = ""

textDict = json.loads(dataInputParameter)

for employee in textDict['employees']:
varaddress = employee["address"]
vardistance = employee["distance"]
varid = employee["id"]

print ("address: " + varaddress)
print ("distance: " + vardistance)
print ("uniqueid: " + varid)
print ""

msg1 =("MESSAGE Variable 1: " + dataInputParameter1)
msg2 =("MESSAGE Variable 2: " + dataInputParameter2)
msg3 =("MESSAGE Variable 3: " + dataInputParameter)
msg4 = (msg1 + msg2 + msg3)
arcpy.AddMessage(msg4)
resultMsg = msg4
arcpy.SetParameterAsText(1, resultMsg)

Executing: CreateMultiPolyBatch '{"employees":[{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9",  "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9",  "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9",  "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"113 Buffalo Rd, Clarksville, Virginia(VA), 23927","distance":"50",  "id" : "4" },{"address":"8817 Sherando Dr, Bristow, Virginia(VA), 20136","distance":"20",  "id" : "5" }]}'

Start Time: Mon Mar 30 16:08:57 2020
Running script CreateMultiPolyBatch...

MESSAGE Variable 1: '{"employees":[{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"113 Buffalo Rd, Clarksville, Virginia(VA), 23927","distance":"50", "id" : "4" },{"address":"8817 Sherando Dr, Bristow, Virginia(VA), 20136","distance":"20", "id" : "5" }]}'

MESSAGE Variable 2: '{"employees":[{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"113 Buffalo Rd, Clarksville, Virginia(VA), 23927","distance":"50", "id" : "4" },{"address":"8817 Sherando Dr, Bristow, Virginia(VA), 20136","distance":"20", "id" : "5" }]}'

MESSAGE Variable 3: {"employees":[{"address":"1520 Split Oak Ln, Henrico, Virginia, 23229","distance":"10", "id" : "1" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"1341 Research Center Dr Blacksburg, VA 24060","distance":"9", "id" : "2" },{"address":"816 W Main St, Danville, Virginia(VA), 24541","distance":"12", "id" : "3" },{"address":"113 Buffalo Rd, Clarksville, Virginia(VA), 23927","distance":"50", "id" : "4" },{"address":"8817 Sherando Dr, Bristow, Virginia(VA), 20136","distance":"20", "id" : "5" }]}

Completed script CreateMultiPolyBatch...
Succeeded at Mon Mar 30 16:08:57 2020 (Elapsed Time: 0.11 seconds)
0 Kudos
jaykapalczynski
Frequent Contributor

I think I might have it....I copied the string and remove the ' on the beginning and end and I think I can now pass it as a parameter...

Still testing

0 Kudos
jaykapalczynski
Frequent Contributor

Yup....syntax just the ' on front and backside.....everything seems to be working.....thank you all for your thoughts and pointers....cheers....stay safe

0 Kudos