How to solve : "Bad control character in string literal in JSON" ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2025 10:45 PM
Hi All,
I am trying to pass rejection comments through JSON once the RITM state is rejected.
The JSON payload is :
But I am getting the below error in the response body:
How can I solve this issue?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2025 11:12 PM
some invalid character is being transferred and hence the error
just before including comments, strip the invalid characters
see this
JSON encoding - String contains control character
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2025 11:50 PM
Hi @User205031
----------------------------
{
"decision": "rejected",
"ritm": "RITM005348616",
"comment": "11/04/2025 01:22:26 AM - Ravi (...) rejectingggggggggggggg\n"
}
Always wrap your JSON in JSON.stringify() rather than manually concatenating strings:
var payload = {
decision: "rejected",
ritm: "RITM005348616",
comment: "Rejected due to missing details"
};
var requestBody = JSON.stringify(payload);
-------------------
If you’re building the JSON dynamically in ServiceNow, use:
var payload = {};
payload.decision = "rejected";
payload.ritm = current.number.toString();
payload.comment = gs.nowDateTime() + " - " + gs.getUserDisplayName() + " (Comments) " + comments.replace(/\r?\n/g, "\\n");
var body = JSON.stringify(payload);
That .replace(/\r?\n/g, "\\n") ensures any line breaks in the comment field are properly escaped before converting to JSON.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2025 12:35 AM
Hi @Ravi Gaurav ,
I have written the below script in the 'Script Step' section:
How do I modify the code?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2025 12:41 AM
update as this -> why are you using stringify when you simply want comments text
outputs.comment = rejectionComments;
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏