Reliability issue

Unsafe JSON parsing in n8n

Why JSON.parse without error handling can crash your entire workflow

What is this issue?

Using JSON.parse() directly without a try/catch block is dangerous because it throws an exception when given invalid JSON. This can crash your entire workflow if the incoming data isn't perfectly formatted.

Common scenarios that cause failure:

  • API returns error message instead of JSON
  • Truncated response due to network issues
  • XML or HTML returned instead of JSON
  • Empty string or null values

Why is this dangerous?

Workflow crashes

A single malformed JSON response will crash the entire workflow execution.

Data loss

All items being processed are lost when the workflow aborts.

Intermittent failures

The workflow might work 99% of the time but fail unexpectedly when data is malformed.

Hard to debug

JSON parse errors can be cryptic, making it difficult to understand what went wrong.

How to fix it

  1. 1

    Wrap in try/catch

    Always wrap JSON.parse() in a try/catch block to handle invalid JSON gracefully.

  2. 2

    Validate before parsing

    Check if the string looks like valid JSON before attempting to parse it.

  3. 3

    Provide fallback values

    Return a default object or null when parsing fails instead of crashing.

  4. 4

    Log parsing errors

    Log the original string when parsing fails to help debug data format issues.

Scan your workflow now

Upload your n8n workflow JSON and detect unsafe JSON parsing patterns in your Code nodes.

Scan for reliability issues

Related resources

Related reliability issues