GP Service Ideas to output a .json file

1127
1
Jump to solution
09-21-2017 07:23 AM
JamesCrandall
MVP Frequent Contributor

EDIT: Turns out the .py script example I have below does actually create the .json file with the input parameter value.  I'm just not correctly handling the JavaScript side to allow user to "SaveAs".

Wild idea but *should be* straight forward.  I'd like to publish a GP service that simply takes an input string (well formatted json), write that string to a .json file (just a text file would do with a .json extension I think) and set this to an output parameter.

What I have so far doesn't error out but also doesn't seem to generate the output .json I'm expecting.

Simple python script source to the GP tool:

import os
import arcpy
import uuid

#GP tool input parameter
inSessionStr = arcpy.GetParameterAsText(0)

#write a to a temp .json file (taken from the enhanced print service blog
output = 'SessionFile_{}.{}'.format(str(uuid.uuid1()), "json")
Output_File = os.path.join(arcpy.env.scratchFolder, output)

#write to the .json file
outputSessionfile = open(Output_File, 'w')
outputSessionfile.write(inSessionStr)

#specify the output parameter as the outputSession file
arcpy.SetParameter(1, outputSessionfile)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any ideas on if this might work and what to look for?

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

Again, the python script works as intended and I just wanted to post the full solution.

GP Tool Source Script:

import os
import arcpy
import uuid


#GP tool input parameter
inSessionStr = arcpy.GetParameterAsText(0)

#write a to a temp .json file (taken from the enhanced print service blog
output = 'SessionFile_{}.{}'.format(str(uuid.uuid1()), "json")
Output_File = os.path.join(arcpy.env.scratchFolder, output)

#write to the .json file
outputSessionfile = open(Output_File, 'w')
outputSessionfile.write(inSessionStr)

#specify the output parameter as the outputSession file
arcpy.SetParameter(1, outputSessionfile)

What the GP Tool looks like:

Parameters:

inputSession: String

returnFile: File

JavaScript implemented in a widget:

executeGPDownloadFile: function (param) {
             var gp = new Geoprocessor("https://mydomain/myserver/rest/services/GPToolName/GPServer/GPTaskName");
             var params = { "inputSession": param };

             gp.execute(params, downloadFile);

             function downloadFile(results, messages) {
                 //do something with the results                    
                 var theSessionFile = results[0].value;
                 window.open(results[0].value.url);                   
             }
         }‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

1 Reply
JamesCrandall
MVP Frequent Contributor

Again, the python script works as intended and I just wanted to post the full solution.

GP Tool Source Script:

import os
import arcpy
import uuid


#GP tool input parameter
inSessionStr = arcpy.GetParameterAsText(0)

#write a to a temp .json file (taken from the enhanced print service blog
output = 'SessionFile_{}.{}'.format(str(uuid.uuid1()), "json")
Output_File = os.path.join(arcpy.env.scratchFolder, output)

#write to the .json file
outputSessionfile = open(Output_File, 'w')
outputSessionfile.write(inSessionStr)

#specify the output parameter as the outputSession file
arcpy.SetParameter(1, outputSessionfile)

What the GP Tool looks like:

Parameters:

inputSession: String

returnFile: File

JavaScript implemented in a widget:

executeGPDownloadFile: function (param) {
             var gp = new Geoprocessor("https://mydomain/myserver/rest/services/GPToolName/GPServer/GPTaskName");
             var params = { "inputSession": param };

             gp.execute(params, downloadFile);

             function downloadFile(results, messages) {
                 //do something with the results                    
                 var theSessionFile = results[0].value;
                 window.open(results[0].value.url);                   
             }
         }‍‍‍‍‍‍‍‍‍‍‍‍