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
Monday
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
Monday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
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.
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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
yesterday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
