n8n JSON Parse Errors

Step by Step Guide to solve n8n JSON Parse Error

 

 


 

Who this is for: n8n developers who integrate external APIs and need a reliable way to handle malformed JSON responses. We cover this in detail in the n8n API Integration Errors Guide.


Quick Diagnosis: Stop your n8n workflow from crashing on bad JSON

  • Set the HTTP node’s Response Format to RAW for unreliable APIs.
  • Wrap JSON.parse in a Function node with try…catch.
  • Validate only after you’ve sanitized the payload (remove stray commas, strip BOM, etc.).
  • Use an Error Trigger to log and notify when parsing fails, keeping the main flow alive.

Why n8n Throws a “JSON Parse Error” ?

Symptom Typical Root Cause Node Involved
Unexpected token < … API returned HTML (e.g., 404 page) HTTP Request
Unexpected end of JSON input Truncated response (size limit, timeout) HTTP Request
Unexpected token … (any other char) Malformed JSON – stray commas, missing quotes, BOM JSON Parse / Set
Cannot read property ‘…’ of undefined Down‑stream node expected a JSON object that never materialised Any downstream node

The exact error text tells you whether the issue originates from the source API or from node configuration.


Step‑by‑Step Debugging Guide

Overview

We’ll capture the raw response, inspect it, clean common issues, safely parse it, and finally set up centralized error handling.

1️⃣ Capture the Raw Response

Purpose: Store the unprocessed payload so the workflow can continue even if parsing fails.

{
  "rawResponse": "{{$node["HTTP Request"].json}}"
}

*Add a Set node after the HTTP Request and map the expression above to rawResponse.*

2️⃣ Inspect the Payload (Quick Test)

Purpose: Verify what the API actually returned.

// Code node: print first 500 characters
const raw = $json.rawResponse;
console.log("First 500 chars:", raw.slice(0, 500));
return [{ inspected: true }];

Run the workflow in Execute Workflow mode and review the console output. HTML tags or broken characters indicate a source‑side problem.

3️⃣ Clean & Sanitize the Payload

3.1 Remove BOM and trailing commas

// Function node: basic cleaning
let raw = $json.rawResponse;
raw = raw.replace(/^\uFEFF/, "");          // strip BOM
raw = raw.replace(/,\s*}/g, "}");          // trailing commas before }
raw = raw.replace(/,\s*]/g, "]");          // trailing commas before ]

3.2 Safe JSON parsing with fallback

// Function node: try/catch parse
let parsed;
try {
  parsed = JSON.parse(raw);
} catch (e) {
  // Fallback object for downstream handling
  parsed = { __parseError: true, raw };
}
return [{ parsed }];

*If you prefer a no‑code approach, feed raw into a built‑in JSON Parse node and enable **Continue on Fail**.*

4️⃣ Centralised Error Handling

Purpose: Capture parsing failures, log the raw payload, and alert the team.

{
  "errorDetected": "{{$json.parsed.__parseError === true}}"
}

– Add an Error Trigger node at the top of the workflow.
– Connect it to an IF node that checks errorDetected.
– Route true‑branch items to Slack, Email, or a logging destination (Google Sheet, DB, etc.).

EEFA Warning – Never silently swallow parsing errors in production. Always log the raw payload and notify the responsible team.


Real‑World Example: Fixing a 3rd‑Party Weather API

The API returns malformed JSON:

{
  "temperature": 23,
  "conditions": "Sunny",,
}

3.1 Clean the stray comma

// Function node: remove double comma before }
let raw = $json.rawResponse;
raw = raw.replace(/,\s*}/g, "}"); // fixes the extra comma

3.2 Parse the cleaned string

// Function node: parse after cleaning
const parsed = JSON.parse(raw);
return [{ weather: parsed }];

After these two nodes, the workflow stores the weather data in Airtable without interruption.


Checklist – Prevent Future JSON Parse Errors

  • [ ] Set HTTP node to RAW for unreliable endpoints.
  • [ ] Log the raw payload before any parsing attempt.
  • [ ] Sanitize common malformed patterns (BOM, trailing commas).
  • [ ] Wrap JSON.parse in try/catch or enable **Continue on Fail**.
  • [ ] Add an Error Trigger that routes failures to a monitoring channel.
  • [ ] Validate Content‑Type: application/json header; treat missing header as raw.
  • [ ] Configure a reasonable timeout on the HTTP node to avoid truncated responses.

Conclusion

By capturing the raw response, sanitizing common JSON defects, and handling parsing errors centrally, you turn a cryptic “JSON Parse Error” into a predictable, recoverable condition. This approach keeps your n8n automations robust, production‑ready, and easy to monitor.

Leave a Comment

Your email address will not be published. Required fields are marked *