using GP service message parameter to return json data

3605
4
Jump to solution
04-28-2016 09:51 AM
MaximeDemers
Occasional Contributor III

Is it a good practice to use the GP service message parameter to return json data as string when calling the GP from javascript client through REST?

arcpy.AddMessage(json.dumps(data))
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BillDaigle
Occasional Contributor III

I don't use model builder, but I think I can point you in the right direction.  You need to add an output parameter to your model,

then do something along these lines:

arcpy.setParamterAsText(1,feature_set.JSON)  
#or 
arcpy.setParameter(1,feature_set) 

View solution in original post

4 Replies
BlakeTerhune
MVP Regular Contributor

This got posted to the Python section. You're better off in ArcGIS API for JavaScript

EDIT:

Just noticed your other similar thread. Can python GP services returns json data to client through REST?

0 Kudos
BillDaigle
Occasional Contributor III

I don't see why not.  When I create gp services for web applications, I generally use a single JSON string input and output which makes setting up parameter inputs and processing outputs easy.  I don't see why messages would be any different.  Just make sure you know your data.  If you are stringifying large objects (including messages), you are going to see a lot of data going across the pipe.  

MaximeDemers
Occasional Contributor III

@Bill Daigle

Thank you for your answer. Maybe you could help me a little further. I use a little python script at the end of my model builder to return the data in json format like that:

    import arcpy

    FC_In = arcpy.GetParameterAsText(0)

    feature_set = arcpy.FeatureSet()

    feature_set.load(FC_In)

    arcpy.AddMessage(feature_set.JSON)

In the client I call the GP Service with ajax and I have noticed that the response contains two members

    {

       messages: [ ],

       results: [ ]

    }

In the messages member I have the json data from my GP, but results is an empty array. Why does the results empty? Would not be better to pass the json data inside the results parameter instead of the messages parameter? How can I do that?

0 Kudos
BillDaigle
Occasional Contributor III

I don't use model builder, but I think I can point you in the right direction.  You need to add an output parameter to your model,

then do something along these lines:

arcpy.setParamterAsText(1,feature_set.JSON)  
#or 
arcpy.setParameter(1,feature_set)