New Line problem when sending JSON through HTTP

I’m sending a JSON through an HTTP node to ManyChat but the new lines dont activate in the whatsapp message

This is what i’m sending:

{{ $json.message.content.part1.replaceAll(‘\n’,‘\n’).toJsonString() }}

I’ve tried all type of combinations, I’m not sure if it’s a ManyChat problem or what.

Please help!

Your expression treats the json “string” as a JS object, so the .toJsonString() part will strip out all whitespace, deal with escaping special characters like quotes, etc. If you want the output message to be formatted (i.e. “pretty printed”), you could use this expression instead:

   JSON.stringify({{ $json.message.content.part1 }}, null, 2)

You should still be aware that HTML and JSON are whitespace (tabs,spaces,linefeeds,etc.) agnostic and the choice of how to render/show the content on screen is ultimately up to the parser/display-client. Be sure you’re not fighting a battle you can’t win (i.e. test to see whether Whatsapp is reformatting your JSON no matter what you do to it.) Also, be sure you’re not expecting an actual linefeed within a string value to be retained. Within a string value, to keep a newline/linefeed character, the slash character might need to be escaped instead, like "My first line of text\\nMy second line of text."

1 Like