Handling Special Characters in Scripted REST API Payload

Priya3
Tera Contributor

Hi,
I have created a Scripted REST API to generate requests in ServiceNow. However, when special characters are passed in the payload, the field values are stored as encoded entities such as à, ', &, etc.
Currently, I am using the following logic:
var RequestBody = request.body;
var RequestData = RequestBody.data;
var strREQ = JSON.stringify(RequestData);
var parser = new JSONParser();
var result = parser.parse(strREQ);
for (var key in result) {
if (key != 'att_data' && result[key] && typeof result[key] === 'string') {
result[key] = GlideStringUtil.unEscapeHTML(result[key]);
}
}
This approach is able to decode basic HTML entities like &, but it does not handle numeric character references such as à or '.
Could you please advise on the best approach to decode these numeric values in ServiceNow?
Thanks,

4 REPLIES 4

Tanushree Maiti
Kilo Patron

Hi @Priya3 

 

Please ref: How to allow special characters from JSON payload data

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@Priya3 

try this

function decodeHtmlEntities(str) {
    if (!str)
        return str;

    str = str.replace(/&/g, '&')
             .replace(/&lt;/g, '<')
             .replace(/&gt;/g, '>')
             .replace(/&quot;/g, '"')
             .replace(/&apos;/g, "'")
             .replace(/&#39;/g, "'");

    str = str.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(parseInt(dec, 10));
    });

    str = str.replace(/&#x([0-9a-fA-F]+);/g, function(match, hex) {
        return String.fromCharCode(parseInt(hex, 16));
    });

    return str;
}

var requestBody = request.body.data;
var strREQ = JSON.stringify(requestBody);
var parser = new JSONParser();
var result = parser.parse(strREQ);

for (var key in result) {
    if (key != 'att_data' && result[key] && typeof result[key] === 'string') {
        result[key] = decodeHtmlEntities(result[key]);
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Priya3 

Thank you for marking my response as helpful.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

palanikumar
Giga Sage

Hi @Priya3 

 

You can try this

 

var RequestBody = request.body;
var RequestData = RequestBody.data;
var strREQ = JSON.stringify(RequestData);
var parser = new JSONParser();
var result = parser.parse(strREQ);
for (var key in result) {
  if (key != 'att_data' && result[key] && typeof result[key] === 'string') {
    result[key] = decodeHTML(result[key]);
  }
}

function decodeHTML(encodedString) {
    return (j2js(new GlideStringUtil().unEscapeHTML(encodedString))).replace(/&#x{0,1}([0-9a-fA-F]+);/gi, function(match, numStr) {
		gs.info(numStr.toString());
        var num = parseInt("0x" + numStr.toString());
        return String.fromCharCode(num);
    });
}

 

Thank you,
Palani