n8n LabIntermediate
Avoid Errors on Missing Fields with Optional Chaining in n8n
Some leads have no address object, which makes your expression throw 'Cannot read properties of undefined'.
Live Execution Simulator
Idle
π₯
Incoming Data
βοΈ
n8n Node
π―
Next Step
> Ready. Type your expression below and click "Check Answer" to run the node.
Incoming Payload / Data:
{
"customer": {
"name": "Omar"
}
}
$json.customer
?? 'Unknown'
βοΈ Your expression is executed against test data β any correct solution passes. +20 XP
β Solution & explanation
$json.customer?.address?.city ?? 'Unknown'
Output: Unknown
?. stops and returns undefined instead of throwing when the value on its left is missing. ?? then supplies the fallback.π‘ Pro tip: Optional chaining also works on arrays and functions: $json.items?.[0]?.name.
π ?. (optional chaining) reference: syntax & more examples →