Fixing JSON Errors in n8n HTTP Requests

Learn how to resolve invalid JSON errors in n8n HTTP Request nodes when sending messages via TNZ.

TNZ provides SMS, email and messaging APIs.

When integrating TNZ’s messaging services with n8n (a workflow automation tool), you may occasionally encounter errors where the HTTP Request node fails to send data due to a JSON formatting error.

An example is:

JSON parameter needs to be valid JSON


A JSON object is a clean, escaped block of code. When you map dynamic data, often it contains special characters (like quotes, newlines, or backslashes). If n8n passes these characters directly into the HTTP request's JSON without "escaping" them, the JSON breaks, resulting in a failed request.

This guide explains why this happens and provides two reliable methods to format your payloads correctly using the HTTP Request node in n8n.

The Problem: Unescaped Characters

Standard JSON format relies on double quotes " to define keys and values. If your dynamic content also contains quotes, the system cannot distinguish between the JSON structure and your content.

Quote Characters

Imagine you are pulling a Business Name field from a previous node.

Business Name:

The "Darling" on Main Street

Resulting incorrect JSON:

{
  "message": "Hello The "Darling" on Main Street, your appointment is confirmed."
}

The quotes around "Darling" tells the system the string has ended, making the rest of the text invalid syntax.

Corrected JSON:

{
  "message": "Hello The \"Darling\" on Main Street, your appointment is confirmed."
}

The correct JSON uses backslash escape characters in front of the quotes.

Line Breaks

Imagine you are pulling a Registration Notes field from a previous node.

Registration Notes:

I operate a local theatre.
Please contact me regarding cleaning.


Resulting incorrect JSON:

{
  "message": "A new enquiry has been received:\n I operate a local theatre.
Please contact me regarding cleaning." }

The "message" doesn't have an ending quote, and there's a new line with no starting quote. The entire string is invalid syntax.

Corrected JSON:

{
  "message": "A new enquiry has been received:\n I operate a local theatre.\nPlease contact me regarding cleaning."
}

The correct JSON replaces line breaks with \n characters.

Where This Appears in Real Workflows

  • Sending SMS notifications
  • Appointment reminders
  • Monitoring or system alerts
  • Form submissions to customers
  • Staff or field team notifications

TNZ supports these messaging use cases through SMS, Email and Voice APIs designed for automation tools like n8n.

Solution 1: Use Fields in the HTTP Request Node

The easiest option is to use the Using Fields Below option when configuring your HTTP Request node.

n8n will automatically escape the field values for you.

n8n Send HTTP Request - Use Fields Below [Marked]

Sending messages from n8n? TNZ provides SMS, email and voice APIs that work cleanly with the HTTP Request node.

Solution 2: The "Whole Object" Method

The most robust way to handle dynamic data in n8n is to treat the entire JSON body as a JavaScript object.

By wrapping the entire block in double curly braces {{ }}, you tell n8n to process the content as JavaScript code rather than raw text.

This method automatically handles data types and avoids most escaping issues.

How to apply it:

  1. Open your HTTP Request node.

  2. Set Body Content Type to JSON.

  3. Toggle the Body parameter type to be Expression.

  4. Enter your payload as a JavaScript Object inside {{ }}.

Code Snippet:

{{
  {
    "sender": "MyCompany",
    "message": "The contact's name is: " + ($('Previous_Node').item.json.contact_name || "Valued Customer"),
    "targets": [$('Previous_Node').item.json.mobile_number]
  }
}}

Note: In this method, do not put quotes around the variable names or keys unless they are part of the string value. It is pure JavaScript. The || characters define the value to use if the field is undefined.

Many TNZ customers use this method when building high-volume SMS or alerting workflows.

Solution 3: The JSON.stringify() Method

If you prefer to maintain the visual structure of a standard JSON block (mixed text and expressions), you can use the built-in JSON.stringify function to tidy up the fields.

You must wrap your fields in JSON.stringify().

If the contact name is John "The Boss", JSON.stringify converts it to "John \"The Boss\"", escaping the internal quotes so the outer JSON remains valid.

How to apply it:

  1. Open your HTTP Request node.

  2. Set the input mode to standard JSON.

  3. Insert an expression {{ }} only for the specific value field, using the JSON.stringify function.

Code Snippet:

{
  "sender": "MyCompany",
  "message": {{ JSON.stringify("The contact's name is: " + ($('Previous_Node').item.json.contact_name || "")) }},
  "targets": ["+6421000000"]
}


JSON escaping errors are a common cause of failed SMS API calls in automation tools.

Summary Table

Feature Use Fields Method Whole Object Method JSON.stringify Method
Syntax No complex syntax. Pure JavaScript Object. Mixed JSON and JavaScript.
Reliability High. Native n8n drag-and-drop functionality. High. Less prone to syntax errors. Good, but requires manual application per field.
Readability High. Native n8n drag-and-drop functionality. Clean for developers familiar with JS. Easier to read for those preferring JSON structure.
Best For Simple payloads without any nested JSON objects. Complex payloads with multiple dynamic fields. Simple payloads with only one dynamic text field.

Still experiencing issues?

If you are still receiving 400 Bad Request or JSON Parse Error responses after trying these methods, please double-check that your previous nodes are outputting the data in the format you expect.

Building messaging workflows in n8n?

If you're using n8n for notifications, reminders or alerts, TNZ provides:

All accessible via HTTP requests like the examples in this guide.


Topics:   Developers

Start your free trial today!

Ready to revolutionise your messaging?
Connect with people anywhere - boost engagement, responses and conversations.

20 credits over 14-days. No obligation. No credit card required.