- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2018 01:42 PM
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.
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2018 03:17 PM
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":
*Please mark as Helpful or Correct Answer if this was useful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2018 03:14 PM
/* 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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2018 03:31 PM
Thank you. I've tried similar. Unfortunately as soon as it's placed in the REST response it is escaped.
"{\"records\":[{\"value\":{\"short_description\":\".........

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2018 03:17 PM
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":
*Please mark as Helpful or Correct Answer if this was useful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2018 08:43 AM
did you check the respons JSON with
var httpResp = JSON.stringify(JSON.parse(response.getBody())); ?