Output json from Geoprocessing script?

2269
2
Jump to solution
06-06-2014 08:29 PM
NeoGeo
by
Occasional Contributor III
I do not see any way to make a 10.2 geoprocessing script return data in json format (no json option in output parameter data types).  Is there some other multiple value data structure that a geoprocessing script can handle that I can use to output my data?

What I have tried, is setting the output parameter to multivalue with type "any value" and using this code:            
results=arcpy.SetParameter(5,';'.join(record_list))

but it fails saying it is expecting strings because my data is a list of dictionaries not a list of strings. 

My output data is in this format:
[{'layer': 'Wombat Habitat', 'value': '-999'}, {'layer': 'Purple People Eater Habitat', 'value': '7.65'}, {'layer': 'Cookie Monster Habitat', 'value': '3.03'}]

Background info:  This was originally a standalone REST service which output json that was consumed by a Flex widget that I have rewritten as a Python script because previous versions are no longer supported for various reasons I won't get into here.  I wrote the new Python version as a standalone RESTful service but was running into problems with ArcPy memory leaks, which would not matter if the script ended but are a major problem for a service that does not end.  At this point I am trying to just make it into a Geoprocessing script to be published as a service and just modify the widget to consume from thatGP service instead of a normal standalone REST service because I am guessing the GP service will execute it as if it was a normal script which would end (therefore getting memory back).  In case you are wondering about the memory leaks, I did try using del statements, with statements, and putting as much as I could in functions to free up memory but it just keeps growing every time the service gets called.

Thank you!
0 Kudos
1 Solution

Accepted Solutions
NeoGeo
by
Occasional Contributor III
Actually, the script was using json.dumps from the beginning with string as the output type, but when I ran it initially, there was no output so I had removed the json conversion and started looking at using some other form of data structure.  Basically the problem came down to not understanding the ESRI way of doing things even after reading the docs.  The json as a string works fine.  The reason the output was empty was simply because I did not know you had to use setparameter no matter what.

I will mark your post as helpful because even though it was not the answer, it put me back on the right track, so thank you!

View solution in original post

0 Kudos
2 Replies
Luke_Pinner
MVP Regular Contributor
Set your output as string and use the json.dumps function - https://docs.python.org/2/library/json.html

import json
output_str = json.dumps(output_dict)
0 Kudos
NeoGeo
by
Occasional Contributor III
Actually, the script was using json.dumps from the beginning with string as the output type, but when I ran it initially, there was no output so I had removed the json conversion and started looking at using some other form of data structure.  Basically the problem came down to not understanding the ESRI way of doing things even after reading the docs.  The json as a string works fine.  The reason the output was empty was simply because I did not know you had to use setparameter no matter what.

I will mark your post as helpful because even though it was not the answer, it put me back on the right track, so thank you!
0 Kudos