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

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
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.
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:
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.
There are two reliable ways to fix this in a real n8n workflow.
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.
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.