How to pass Json objects in Rest Post method without quotes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2017 05:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2017 11:46 PM
Anybody who can help me on this??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2017 12:16 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2017 12:28 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2017 12:32 AM
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
