We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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,

5 REPLIES 5

Tanushree Maiti
Tera Patron

Hi @Priya3 

 

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

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

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

@Priya3 

Hope you are doing good.

Did my reply answer your question?

💡 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