arcpy.SetParameter() - Problem passing escaped string as geoprocessing service output

1264
16
12-19-2016 12:14 PM
MaximeDemers
Occasional Contributor III

I have a geoservice that get some text stored from a table in order to return a json string to a javascript client. However, the arcpy.SetParameter() seems to have problem to returns the escaped new lines character (\\n)

my_dict = {"key1" : "test is a string with\nsome escaped characters\nsuch newlines"} 
my_json = json.dumps(my_dict)
print(my_json)
>>> '{"key1": "test is a string with\\nsome escaped characters\\nsuch newlines"}'

arcpy.SetParameter(0, my_json)

in the javascript console the response is the following which is not a valid json:

{"key1" : "test is a string with
some escaped characters
such newlines"}

Is there a solution?

0 Kudos
16 Replies
DanPatterson_Retired
MVP Esteemed Contributor

does the parameter that you are setting normally accept multiline input? perhaps setparameter removes what isn't needed or required

raw formatting doesn't work either I suspect either

0 Kudos
MaximeDemers
Occasional Contributor III

Hi Dan,

It seems its an issue with the SetParameter(). I can't find any solution for this case. Is this should be reported to Esri support?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Well I can't figure it out... so it either something we are missing or something wrong.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Did you try raw formatting the string like Dan suggested?  If so, what were the results?

0 Kudos
MaximeDemers
Occasional Contributor III

Using raw strings does not help.

my_dict = {"key1" : r"test is a string with\nsome escaped characters\nsuch newlines"} 
my_json = json.dumps(my_dict)
print(my_json)
>>> '{"key1": "test is a string with\\\\nsome escaped characters\\\\nsuch newlines"}'

arcpy.SetParameter(0, my_json)


Same problem with the json in the javascript console:
{"key1" : "test is a string with
some escaped characters
such newlines"}

0 Kudos
JamesCrandall
MVP Frequent Contributor
my_dict = {"key1" : "test is a string with\\nsome escaped characters\\nsuch newlines"} 
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It will be interesting if escaping the new lines works.  I won't be totally surprised if it doesn't.  There are at least a couple geoprocessing tools that refuse to work properly with Python new lines.  I usually find a workaround, but I am not sure what that would be in this case.

0 Kudos
MaximeDemers
Occasional Contributor III

@James Crandall : This is not working.

@Joshua Bixby : The work around I have found is to replace the \\n with other characters, for instance <br> before passing it into the SetParameter() and then in the javavscript I replace back the <br> with \n. Off course that's not really satisfying to proceed this way.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Kludgy, but it seems to work for you.  I would open an Esri Support case to log a bug, which this seems to be, but whether stuff like this gets fixed or not is a total crap shoot from my experience.

0 Kudos