/n8n-tutorials

How to fix “unexpected token” error when parsing JSON from Claude in n8n?

Learn how to resolve the 'unexpected token' JSON parsing error from Claude in n8n with simple fixes to ensure clean, valid AI workflow responses.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to fix “unexpected token” error when parsing JSON from Claude in n8n?

The direct fix is: stop trying to JSON-parse Claude’s raw output directly. Claude often returns normal text (with line breaks, quotes, explanations, or Markdown) mixed with your JSON. You must either force Claude to return valid JSON only, or clean the output before running JSON.parse / using the “Convert to JSON” node. In n8n, the most reliable fix is to wrap Claude’s output with a JSON schema prompt and then use a small Function node to extract the JSON block safely before parsing it.

 

Why the error happens

 

In n8n, the JSON parse error “Unexpected token … in JSON at position …” nearly always means that the text you’re trying to parse is not pure JSON. Large language models like Claude often add:

  • Extra words like “Here is your JSON:”
  • Code fences with three backticks
  • Trailing commas or unescaped quotes
  • Markdown formatting
  • Multiple JSON objects in one response

n8n nodes such as Convert to JSON or Function → JSON.parse() expect a clean JSON object or array. Anything else throws the “unexpected token” error.

 

How to fix it (production‑safe approach)

 

There are two reliable ways to fix this in a real n8n workflow.

  • 1. Make Claude output strict JSON only Add this to your system prompt or user instruction:
    “Return only valid JSON. No explanations, no surrounding quotes, no Markdown, no comments. Respond with a single JSON object.”
    This usually prevents dirty output, which is the ideal fix because it avoids error handling downstream.
  • 2. Clean the response before parsing When Claude still adds text, use a small Function node to extract the JSON block. This is extremely common in production.

 

Safe extraction Function node

 

Place a Function node right after Claude’s output. Use this code:

const raw = $json.text || "";   // adjust key if Claude output is in a different field

// Find the first and last curly braces
const start = raw.indexOf("{");
const end = raw.lastIndexOf("}");

// If nothing looks like JSON, stop early
if (start === -1 || end === -1) {
  throw new Error("No JSON object found in Claude output");
}

// Extract substring
const jsonString = raw.substring(start, end + 1);

// Parse it safely
let parsed;
try {
  parsed = JSON.parse(jsonString);
} catch (err) {
  throw new Error("Failed to JSON.parse cleaned Claude output: " + err.message);
}

return [{ json: parsed }];

Why this works: It ignores anything before or after the JSON block (Markdown, comments, backticks, casual text) and isolates only the real JSON object. This is the single most robust method when using LLMs inside n8n.

 

Other important production notes

 

  • Claude can output Unicode smart quotes which break JSON. The extract-and-parse method removes most of the risk.
  • Don’t use the “Convert to JSON” node on raw LLM text. These nodes assume clean data; use them only after extraction or after a reliable structured response.
  • Claude sometimes returns arrays instead of objects. If you want an object, specify it strictly in the prompt.
  • Add an Error Workflow in production so you get notified instead of silently failing.

 

The short version you can tell a junior dev

 

The “unexpected token” error happens because Claude’s reply is not pure JSON even though it looks like it. Clean the output first or force Claude to output strict JSON. The most reliable fix is a Function node that extracts the JSON between the first “{” and the last “}” and then parses it.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022