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?
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
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?
Well I can't figure it out... so it either something we are missing or something wrong.
Did you try raw formatting the string like Dan suggested? If so, what were the results?
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"}
my_dict = {"key1" : "test is a string with\\nsome escaped characters\\nsuch newlines"}
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.
@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.
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.