Sending quotes in REST api request

Saquib Mohammed
Mega Guru

My requirement is to send the full text including any special characters from Incident task description in the json request. json Essentially, I need to escape any special character.

for example, if Description is - This is "Test"

then it should be sent like this in the json request - This is a \”Test\” 

How do I escape the special characters while setting up the request parameter for the json request

I tried setStringParameterNoEscape but in that case, the description in json request is - This is "Test"

 

 

 

3 REPLIES 3

RaghavSh
Kilo Patron

Try below:

var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.replace(/\\n/g, "\\n")
                                      .replace(/\\'/g, "\\'")
                                      .replace(/\\"/g, '\\"')
                                      .replace(/\\&/g, "\\&")
                                      .replace(/\\r/g, "\\r")
                                      .replace(/\\t/g, "\\t")
                                      .replace(/\\b/g, "\\b")
                                      .replace(/\\f/g, "\\f");

 
Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023

Ankur Bawiskar
Tera Patron
Tera Patron

@Saquib Mohammed 

try to replace the double quote with html encoding

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I was able to use regex to do what is needed for quotes - replace(/"/g, '\\"');

However, this doesn't work for escaping \

I need to send it like \\

Any pointers?

Also, how do I escape backspace character? Regex treats \b are word break - I want to escape backspace character (hex character code 08). If i use \x0B, business rule script doesn't accept it and  treats it as unexpected.