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.
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.
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.
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.
Recommended Steps
- Open the failing execution and copy the exact input to the node that reports invalid JSON.
- Check whether the body is built as structured fields, raw JSON text, or Code output.
- Look for trailing commas, unescaped quotes, missing braces, or expressions inside quoted strings.
- Use Set before the HTTP Request node to normalize optional fields and defaults.
- 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
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.