Flow Designer REST step JSON parsing error due to newline (\n) in script output

ravish2k
Tera Contributor

Hi All,

I'm facing an issue in Flow Designer when passing a value from a Script step into a REST step.

I am building a string using incident task fields like below:

var summary = fd_data.subflow_inputs.incident_task.description + "\n" +
fd_data.subflow_inputs.incident_task.number;

 

Return summary:

This value is then used in the REST step request body.

However, whenever the string contains a newline (\n), the REST step fails with the error:

"There was an error parsing JSON. Check that your request body is valid."

If I remove the newline, it works fine.

 

Questions:
1. How should newline characters be handled in Flow Designer when building JSON payloads?
2. Should the newline be escaped differently (e.g. \\n)?
3. Is there a recommended way to safely pass multi-line text to REST APIs from Flow Designer?

 

Any guidance would be appreciated.

 

Thanks!

Ravish

3 REPLIES 3

lauri457
Tera Sage

There are a few control characters that need to be escaped for valid json. You can use the glidestringutil api. Or just use the json builder step in your action

GlideStringUtil.escapeNonPrintable("a\n\t\b\''''")

You can also use the transform functions on data pills

Screenshot 2026-03-30 095811.png

Ankur Bawiskar
Tera Patron

@ravish2k 

you can either replace those or try to use JSON.stringify()

share your complete code here

💡 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

Tanushree Maiti
Kilo Patron

Use the built-in JSON.Strinfy() method.

 

Sample code:

var reqPayload = {
    "fieldName": "Line 1\nLine 2"};
var jsonReqPayload = JSON.stringify(reqPayload);

 

 

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