JSON Safe Input in HTTP Request

Describe the problem/error/question

I use the HTTP Request Node with “Send Body” Using JSON with this JSON:

{ “useridentifier”: “{{ $(‘BeekeeperUser’).item.json.tenantuserid }}”, “recipient”:“[email protected]”, “departmentid”: “{{ $(‘CleanRequest’).item.json.departmentID }}”, “subject”: “{{ $(‘CleanRequest’).item.json.subject.replace(/(\r\n|\n|\t|\r)/gm, “”) }}”, “message”: “{{ $(‘CleanRequest’).item.json.message.replace(/(\r\n|\t|\n|\r)/gm, “”) }}”, “recipient_name”: “Philipp”, “status”: “N”, “mail_message_id”: “string”, “do_not_send_mail”: “N”, “use_template”: “Y”, “is_html_message”: “N”, “custom_fields”: [ { “code”: “kategorie”, “value”: “{{ $(‘CleanRequest’).item.json.requestCategory }}” }, { “code”: “form”, “value”: “{{ $(‘CleanRequest’).item.json.requestForm }}” }] }

This works for most cases, but sometimes Users add weird characted into the message and then it gives me “JSON parameter need to be an valid JSON” Error. Thats why I added .replace(/(\r\n|\t|\n|\r)/gm to replace some invalid characters, but I keep getting this error still from time to time for new messages. It works for most messages without problems. Is there a good general way how I can solve this for whatever data the users enters?

Its on the cloud n8n.

Any help would be greatly appreciated.

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Philipp , I do not think you need to substitute new lines. Just make the whole JSON an expression as in

{{
    {
        "useridentifier": $('BeekeeperUser').item.json.tenantuserid,
        . . .
    }
}}