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.
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
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.