Tips > Data, APIs & Webhooks

Handle Empty and Null Values with Fallback Expressions

APIs return incomplete data constantly -- optional fields come back as `null`, empty strings, or are missing entirely.

APIs return incomplete data constantly -- optional fields come back as null, empty strings, or are missing entirely. Without fallbacks, a single null value can crash downstream nodes that expect a string. Defensive expressions cost almost nothing and prevent entire workflow failures.

Real-world example: A contact form webhook sometimes omits the company field. A downstream Slack message node uses that field in its text. Without a fallback, the message shows "undefined."

// Basic fallback with ||
{{ $json.company || "No company provided" }}

// Nullish coalescing for when 0 or "" are valid values
// (|| treats 0 and "" as falsy; ?? only catches null/undefined)
{{ $json.score ?? 0 }}

// Nested fallback chain
{{ $json.preferred_name || $json.first_name || "Valued Customer" }}

// Conditional with ternary
{{ $json.is_premium ? "Premium Support" : "Standard Support" }}

// Safe property access for deeply nested objects
{{ $json.metadata?.custom_fields?.priority || "normal" }}
```text
Apply this pattern in an Edit Fields node for data cleaning before any critical downstream step:

| Output Field   | Expression                                                  |
|----------------|-------------------------------------------------------------|
| `contactName`  | `{{ $json.name \|\| "Unknown Contact" }}`                   |
| `company`      | `{{ $json.company \|\| "Unaffiliated" }}`                   |
| `priority`     | `{{ $json.metadata?.priority \|\| "normal" }}`              |
| `score`        | `{{ $json.lead_score ?? 0 }}`                               |

> **Tip: Validate Early**
>
> Place a fallback/cleaning Edit Fields node immediately after your data source (webhook, API call, database query). This single "sanitizer" node protects every downstream node from null-related failures.

Adding fallbacks at the data entry point of your workflow prevents cascading errors and makes debugging significantly easier.

**Related:** [Flatten Deeply Nested API Responses](../code-node-mastery/01-flatten-deeply-nested-api-responses.md) | [Use the HTTP Request Node as a Universal Connector](../integration-patterns/01-use-the-http-request-node-as-a-universal-connector.md)

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

191 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book a 20-minute call