JSON parameter needs to be valid JSON with `null`

Describe the problem/error/question

I want to use a field from a Webhook in a JSON payload, which can either be an empty string, or a number. In case it’s an empty string, the JSON payload needs to use null instead.

Originally, I’ve used this expression—which gives the JSON error.

{
  [...]
  "articleId": {{ $('Webhook').item.json.body.ticket.taskarticle !== "" ? $('Webhook').item.json.body.ticket.taskarticle : null) }},
  [...]
}

When POST-ing ‘real’ null like this, the PUT request worked without any issues:

{
  [...]
  "articleId": null,
  [...]
}

For some reason, which I can’t figure out, this works, however:

{
  [...]
  "articleId": {{ $('Webhook').item.json.body.ticket.taskarticle !== "" ? $('Webhook').item.json.body.ticket.taskarticle + "," : JSON.parse("null") + "," }}
  [...]
}

Just using JSON.parse didn’t suffice—I needed to also put the comma into the expression. I’m trying to understand why that’s the case and what would be the correct way to work with a field that could be null or a number.

What is the error message (if any)?

JSON parameter needs to be valid JSON

Information on your n8n setup

  • n8n version: latest
  • Database (default: SQLite): SQlite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian 12

Hey @herzkerl

The expression in your provided JSON - this one:

{{ $('Webhook').item.json.body.ticket.taskarticle !== "" ? $('Webhook').item.json.body.ticket.taskarticle : null) }}

has an additional closing parenthesis) after null and this gives a syntax error. It should be:

{{ $('Webhook').item.json.body.ticket.taskarticle !== "" ? $('Webhook').item.json.body.ticket.taskarticle : null }}

Does that correct the invalid JSON error?

Sorry, that was a typo. This doesn’t work and returns the error:

  "articleId": {{ $('Webhook').item.json.body.ticket.taskarticle !== "" ? $('Webhook').item.json.body.ticket.taskarticle : null }},

Bildschirmfoto 2025-01-12 um 16.49.40

@herzkerl apologies. I should have tested posting that to a test API before adding. I see the same thing.

It does work for me if I use:

{{ $('Webhook').item.json.body.ticket.taskarticle === "" || $('Webhook').item.json.body.ticket.taskarticle === null || $('Webhook').item.json.body.ticket.taskarticle === undefined ? "null" : $('Webhook').item.json.body.ticket.taskarticle }

but I’m sure there is a better way.

The alternative is an if node that checks if taskarticle is empty or not and then set a http request node on the true and false branch, one with the expression and the other null written directly into the JSON.

It seems that if you use the ‘Using Fields Below’ option then the original expression works, it’s just when added to the Using JSON option, null is actually sending as a blank value hence the invalid JSON.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.