error fix

n8n Invalid JSON Error in HTTP Request or Code Nodes

Inspect the exact JSON sent into the failing node, prefer structured fields where possible, validate expression output, and test with real upstream data including missing optional fields.

Match your incident first

Start with the symptom you can prove

Jump to checks

Referenced node did not execute

First check: Open the failed execution and inspect which branch produced the current item.

Wrong fix to avoid: Do not add dummy Set nodes just to silence the error without fixing data flow.

Verify: The expression handles both branch outcomes or references only nodes guaranteed to run.

Field is missing on some items

First check: Pin or inspect multiple real items, not only the first successful item.

Wrong fix to avoid: Do not hardcode array indexes before checking all item shapes.

Verify: The expression uses safe access/defaults and succeeds for empty, partial, and normal items.

Expression works in manual test but fails in production

First check: Compare pinned test data with a recent production execution payload.

Wrong fix to avoid: Do not rely on pinned data as proof of production behavior.

Verify: A fresh production execution with live data passes the same expression.

Use when
n8n workflows, HTTP Request, Code, expressions
First check
Inspect the runtime item shape from the previous node, not just the expression template.
Time to check
5-10 minutes
Next step
Match the symptom, then run the verification checks.

Independent third-party notes. n8n is a trademark of its owner and is referenced only for compatibility and troubleshooting context.

Quick Answer

Inspect the exact JSON sent into the failing node, prefer structured fields where possible, validate expression output, and test with real upstream data including missing optional fields.

Does this match your symptom?

Expression or payload shape is failing

A node receives unexpected data, invalid JSON, missing fields, or an expression works only on one branch.

First check: Inspect the runtime item shape from the previous node, not just the expression template.

Problem Pattern

Invalid JSON errors usually come from hand-built request bodies, expressions that output strings instead of objects, trailing commas, unescaped quotes, or upstream fields that are missing.

Version awareness

Last reviewed 2026-05-21

Key Facts

Common location
Invalid JSON often appears in HTTP Request bodies or Code node transformations.
Expression risk
An expression can produce text that looks like JSON but is not a valid object.
Structured option
Set can normalize fields before a request body is built.
Data-shape risk
A body can be valid with test data and invalid when real fields contain quotes or are missing.

Production Diagnostic Matrix

Turn checks into a brief
Exact symptom or log Likely cause First check Wrong fix to avoid Verification
Referenced node did not execute Expression reads from a node on a branch that did not run for this item. Open the failed execution and inspect which branch produced the current item. Do not add dummy Set nodes just to silence the error without fixing data flow. The expression handles both branch outcomes or references only nodes guaranteed to run.
Field is missing on some items Input items have inconsistent shape, optional provider fields, or previous node output changed. Pin or inspect multiple real items, not only the first successful item. Do not hardcode array indexes before checking all item shapes. The expression uses safe access/defaults and succeeds for empty, partial, and normal items.
Expression works in manual test but fails in production Manual pinned data differs from live payload, timezone, credentials, or branch timing. Compare pinned test data with a recent production execution payload. Do not rely on pinned data as proof of production behavior. A fresh production execution with live data passes the same expression.
Invalid JSON from HTTP Request or Set node String interpolation produced invalid quoting, trailing commas, or unescaped newlines. Validate the exact body sent by the node, not the visual expression preview only. Do not wrap an already-object value in extra quotes. The downstream node receives valid JSON and the response status is expected.
IF branch changes data shape True and false outputs carry different fields, item counts, or nested paths. Inspect both IF outputs and define a shared normalized shape before merge/response. Do not assume the true branch schema exists on false branch items. Both branches feed the next node with the same required fields.
Code node returns wrong item format Code returns a raw object/array instead of an array of items with json fields. Check the Code node return value against the required item format. Do not return secrets or full credentials for debugging. The next node receives items shaped like [{ json: {...} }].
  1. Open the failing execution and copy the exact input to the node that reports invalid JSON.
  2. Check whether the body is built as structured fields, raw JSON text, or Code output.
  3. Look for trailing commas, unescaped quotes, missing braces, or expressions inside quoted strings.
  4. Use Set before the HTTP Request node to normalize optional fields and defaults.
  5. Test with real examples containing missing values, quotes, line breaks, and non-ASCII text.

Verification

  • The failing node receives valid JSON for the original failed payload.
  • The target API accepts the request body and returns the expected status.
  • Optional missing fields do not break the body.
  • Strings with quotes or line breaks remain valid.

Warnings

  • Do not paste user input directly into raw JSON without escaping or structured mapping.
  • A green manual test with one payload does not prove the JSON is safe for all production payloads.
  • Fixing syntax alone may still leave the wrong data type for the target API.

Common Mistakes

  • Building raw JSON by concatenating strings.
  • Putting an object expression inside quotes and turning it into a string.
  • Leaving a trailing comma after the final field.
  • Testing only with clean sample data.

Examples

Safer request-body pattern Normalize fields first, then send structured JSON.
Set: name = {{$json.name || 'Unknown'}}
Set: note = {{$json.note || ''}}
HTTP Request body fields:
  name -> {{$json.name}}
  note -> {{$json.note}}
Avoid: raw string concatenation for JSON

FAQ

Why does JSON validate online but fail in n8n?

The static text may validate, but the runtime expression output can include missing values, unescaped quotes, or a different data type.

Should I use Code to build JSON?

Use Set or HTTP Request structured fields for simple bodies. Use Code when you need complex transformation and can return a real object rather than a JSON-looking string.

Sources