Create JSON request body through Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 03:07 AM - edited 11-20-2023 03:22 AM
Hi,
I want to create JSON request body through script. Can someone help me with the code?
var request = new sn_ws.RESTMessageV2('User Group', 'Group Updation');
request.setStringParameter('sysid' , "a69fc6a21b227dd8df7132e2cd4bcb91");
for (var x in current)
{
if(current[x] != previous[x])
request.setRequestBody('{"'x'" : "'+ current.[x]'"}');
}
I want to loop through each field and if the value of any field is different, i want to add that field name and its current value to the request body. If the values are hardcoded string we can use '\' character but how can we pass variable names and its value in the request body.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 10:19 AM
Hi @RFJ1 ,
You need to escape all the special as well as new line characters your string as JSON does not expect special characters.
var cleanedDescription = current.description.replace(/(\r\n|\n|\r)/gm, "");
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 12:09 PM
Hi @Jim Coyne @Anand Kumar P ,
This was the last I tried,
and got the following response,
How do I remove the " ?
BTW, I tried both your answers and result were not successful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 02:54 PM
I think you are trying too many things at the same time. Forget the "JSON.stringify", and the "replace". Try replacing those 5 lines above with just:
request.setStringParameter('description', encodeURIComponent(current.getValue("description")));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 04:12 PM
The key to successfully use templates is how the parameters are defined.
Usually the template looks something like
{
"parent": "${parent}",
"description": "${description}",
"sys_updated_on": "${sys_updated_on}"
}
This falls on its face as soon as any of the parameters contain invalid JSON value.
It is better to define the template without quotes around string parameters:
{
"parent": ${parent},
"description": ${description},
"sys_updated_on": ${sys_updated_on}
}
In that case one just passes in the stringifyed value as parameter and it will be a valid.
E.g:
var request = new sn_ws.RESTMessageV2();
request.setStringParameterNoEscape("description", JSON.stringify("" + current.description));
Let's say description contains the string
This is a multi-line text
containing quotes: ""'.
Running
JSON.stringify("" + current.description)
for it in Scripts - Background produces the output:
"This is a multi-line text\ncontaining quotes: \"\"."
which if we insert into the template above will result in:
{
"parent": ${parent},
"description": "This is a multi-line text\ncontaining quotes: \"\".",
"sys_updated_on": ${sys_updated_on}
}
and it looks like valid JSON (at least property description does).
Another important bit is to use method setStringParameterNoEscape vs. setStringParameter - the latter will alter the output from JSON.stringify() and can lead to invalid JSON.
In case the parameters are not enclosed between quotes, the otherwise rigid template based request body can be extended with dynamic part, like attaching the changed fields and their values.
Let's say one has built a map like object where properties are fields; e.g:
var changedFields = {
"comments": "A multi-line comment\nthat - as observed - contains \"double\" quotes!",
"sys_updated_on": "2023-11-01 15:00:00"
};
and one wants to attach it to the body.
Say the Content in the HTTP method record is defined as:
{
"parent": ${parent},
"description": ${description},
"sys_updated_on": ${sys_updated_on},
"changedFields": ${changedFields}
}
Again one can execute:
var changedFields = {
"comments": "A multi-line comment\nthat - as observed - contains \"double\" quotes!",
"sys_updated_on": "2023-11-01 15:00:00"
};
// ...
request.setStringParameterNoEscape("changedFields", JSON.stringify(changedFields));
and after that the request body would look as below:
{
"parent": ${parent},
"description": ${description},
"sys_updated_on": ${sys_updated_on},
"changedFields": {"comments":"A multi-line comment\nthat - as observed - contains \"double\" quotes!","sys_updated_on":"2023-11-01 15:00:00"}
}
- again the property at hand is valid and dynamically set JSON.
So, in case of string parameters one should not use quotes and should use JSON.stringify to obtain the parameter value to set.
One can also use the output of JSON.stringify to "inject" entire stringifyed objects in place of parameters.
Also one would do better to use setStringParameterNoEscape (which does not touch the value) vs. setStringParameter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 11:52 PM
@-O- ,
Worked! I have a question, why can't we use this instead of current.description ?
request.setStringParameterNoEscape("description", JSON.stringify("" + current.getValue('description'));