Fix “Unexpected token … in JSON” in n8n
What this error means
JSON.parse() only accepts a JSON string. When you pass it something that is already an object, JavaScript first turns it into the text [object Object] — which isn't JSON, so parsing fails at the second character (the o). Depending on the n8n version, the same problem is reported as "[object Object]" is not valid JSON.
Common causes
- The field is already an object — n8n's Webhook and HTTP Request nodes parse JSON automatically.
- The text isn't valid JSON: single quotes, trailing commas, or HTML returned by an API error page.
- The value is empty, so
JSON.parse('')fails with “Unexpected end of JSON input”.
How to fix it
- Look at the input panel: if the field shows nested keys you can expand, it is already an object — read it directly and remove
JSON.parse. - If the source sometimes sends a string and sometimes an object, check the type first (see the lab below).
- If it's a string but invalid, open the raw value and look for single quotes or an HTML page; fix the source or the request.
- For data that may be broken, parse it in a Code node inside
try { … } catch { … }so one bad item doesn't stop the run.
❌ Breaks
{{ JSON.parse($json.data).plan }}✅ Works
{{ (typeof $json.data === 'string' ? JSON.parse($json.data) : $json.data).plan }}💡 Pro tip: If an HTTP Request returns text you have to parse, set its Response Format to JSON instead — n8n will parse it for you and show a clearer error when the body isn't JSON.
🧪 Fix it yourself
One integration sends `data` as a JSON string, another sends it as an object. Return the plan for both without crashing.
// Source A
{ "data": "{\"plan\":\"pro\"}" }
// Source B
{ "data": { "plan": "team" } }
⚙️ Your fix is executed against both test items — any correct solution passes.
✅ Solution
Output: "pro" and "team"