Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to pass Json objects in Rest Post method without quotes

divyalakshmi
Tera Guru

Hi,

I have a requirement where whenever a record is created in a particular table (eg incident), i should pass the ticket number and other fields in key value pair as json objects like below in rest.

{

  "incidentid": "INC00101",

  "properties": [

      {

          "code": "summary",

          "value": "test incident"

      },

{

"code":"description",

"value":"test desc"

},    

  ]

}

I have configured the Rest Outbound and the outbound requires only two parameters "incidentid" and "properties".

here Properties should push all the filled field names and values. i have written a BR on this table to call the rest outbound and trying to pass summary and application name in key value pair

var property=[];

var json = new JSON();

var r = new sn_ws.RESTMessageV2(incidentRestMessage, 'post');

r.setStringParameter('incidentid', current.number);

//summary

if(current.short_description)

{

var summary={"code":"Summary","value":current.short_description.toString()};

property.push(summary);

}

r.setStringParameterNoEscape('properties',JSON.stringify(property));

var response = r.execute();

var responseBody = response.getBody();

var rBody = r.getRequestBody();

gs.log("rest call for "+current.number +"   "+rBody);

The log returns as {"incidentid": "INC00105","properties":"[{"code":"Summary","value":"heloo"}]"}. But my end tool doesnot accept the quote between properties string and values.

the highlighted quotes should not be sent when passing the parameters. It shoul dbe like {"incidentid": "INC00105","properties":[{"code":"Summary","value":"heloo"}]"}. When i log only JSON.stringfy(property) it returns without quotes but when i render with setStringParameterNoEscape it adds the quotes. Kindly help me on this.

Thanks,

Divya

6 REPLIES 6

divyalakshmi
Tera Guru

Anybody who can help me on this??


Hi Divya,



Try below script




var jproperty=JSON.stringify(property);


r.setStringParameterNoEscape('properties',removeQuotes(jproperty));








function removeQuotes(p){


var firstQuote=p.indexOf('[');


var value=p.slice(0,firstQuote-1)+p.slice(firstQuote,-2)+"}";



return value;


}





I have added a new method to parse the string and remove the quotes from the array position.



Hope it will help.




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


Hi Gaurav,


var jproperty=JSON.stringify(property);


When i log the jproperty gs.log(jproperty) it does not come with quotes. after i render it with r.setStringParameterEscape it comes with quotes.


when i try logging request body gs.log(r.getRequestBody()) it comes with quotes that is the problem now.



Thanks to help


H Divya,



Why don't you add the static body in the REST message definition itself and then just pass the short descript value through setStringparamater?


That will avoid such issues.




Thanks


Gaurav