Prevent REST response from being escaped

rlatorre
Kilo Sage

I have a scripted web service is is generating a JSON formatted string. When sent as the response it is being escaped. How can this be prevented?

Output when run in background script:

{"number": "INC0939032","short_description":"Title text","description":"Description text",....

Output from the REST service:

{\"number\":\"INC0832852\",\"short_description\":\"Title text\",\"description\":\"Description text",....

Requirement:

We need to supply a list of fields and all related comments from and Incident in a single JSON formatted response.

Any assistance will be appreciated.

1 ACCEPTED SOLUTION

Liju John1
Mega Guru

try this .


var httpResp = JSON.stringify(JSON.parse(response.getBody()));


gs.log("REST httpResponse : " + httpResp);


-------------------------------------------------------------------------------------------------



*** Script: REST httpResponse : {"offsets":[{"partition":0,"offset":15938347,"error_code":null,"error":null},{"partition":0,"offset":


find_real_file.png


*Please mark as Helpful or Correct Answer if this was useful.


View solution in original post

5 REPLIES 5

Liju John1
Mega Guru

/* try this.. use   JSON.stringify()*/


try



{



var r = new sn_ws.RESTMessageV2('Kafka REST Endpoint', 'post');



var incident_string = new incidentData().getIncidentRecords(limits);



var temp_string = incident_string;



var request_string = JSON.stringify(temp_string);



gs.log("REST MSG is sent to Kafka : " + request_string);



r.setRequestBody(request_string);



var response = r.executeAsync();



//response.waitForResponse(600);





var httpStatus = response.getStatusCode();



var httpResp = JSON.stringify(response.getBody());



gs.log("REST httpStatus : " + httpStatus);



gs.log("REST httpResponse : " + httpResp);





}



catch(ex) {



var message = ex.getMessage() || ex;



throw message;



}



//************Script Include***************//



var incidentData = Class.create();



incidentData .prototype = {



initialize: function() {



},



getIncidentRecords : function(limits){



var results = {records: []};



var orderBy = 'status';



var limit = limits || 1000;



var incRec = new GlideRecord('incident');



incRec.orderBy(orderBy);



incRec.addEncodedQuery(queryString);



incRec.setLimit(limit);




incRec.query();



while (incRec.next()) {



var obj = {value: {



name : incRec.name.getDisplayValue(),



status : incRec.status.getDisplayValue(),



short_description :   incRec.short_description.getDisplayValue()



} };





for (var key in obj) {



var tmp = {};



tmp[key] = obj[key];



results.records.push(tmp);



}



}



//var json_object = new JSON().encode(results);



return results;




},



type: 'incidentData'



};


Thank you. I've tried similar. Unfortunately as soon as it's placed in the REST response it is escaped.



"{\"records\":[{\"value\":{\"short_description\":\".........


Liju John1
Mega Guru

try this .


var httpResp = JSON.stringify(JSON.parse(response.getBody()));


gs.log("REST httpResponse : " + httpResp);


-------------------------------------------------------------------------------------------------



*** Script: REST httpResponse : {"offsets":[{"partition":0,"offset":15938347,"error_code":null,"error":null},{"partition":0,"offset":


find_real_file.png


*Please mark as Helpful or Correct Answer if this was useful.


Hi Robert LaTorre Jr,


did you check the respons JSON with


var httpResp = JSON.stringify(JSON.parse(response.getBody())); ?