← All articles
DataWeave · MuleSoft · Quick Reads

Removing Null and Empty Fields from JSON in DataWeave

Integration payloads often contain optional fields that a target system does not need.

For example:

{
  "customerId": "C100",
  "email": null,
  "phone": "",
  "tags": [],
  "preferences": {}
}

It is tempting to describe all of these as "empty," but they are not necessarily equivalent.

Before removing anything, decide what the target contract considers meaningful:

null
""
[]
{}

A good DataWeave transformation should encode that decision explicitly.

Remove Null Fields with skipNullOn

When the requirement is simply to omit null values from JSON output, DataWeave provides a concise output directive:

%dw 2.0
output application/json skipNullOn="objects"
---
{
    customerId: payload.customerId,
    email: payload.email,
    phone: payload.phone
}

For this input:

{
  "customerId": "C100",
  "email": null,
  "phone": "555-0100"
}

The output becomes:

{
  "customerId": "C100",
  "phone": "555-0100"
}

This is often preferable to writing a condition beside every optional field when null omission is a general output rule.

Why Omitting Null Can Matter

Some APIs distinguish between:

{
  "email": null
}

and:

{}

The first may mean clear the email. The second may mean leave the existing email unchanged.

That difference is particularly important in update and patch-style integrations.

Do not remove nulls only to make JSON look cleaner. Remove them when that behavior matches the target contract.

Conditional Fields for More Control

Sometimes only selected fields should be omitted.

DataWeave supports conditional object elements:

%dw 2.0
output application/json
---
{
    customerId: payload.customerId,
    (email: payload.email) if (payload.email != null),
    phone: payload.phone
}

This makes the decision local to the field and is useful when null handling differs across the same payload.

For example, perhaps email = null should be omitted while terminationDate = null must be sent because it explicitly clears the target value.

Removing Empty Strings

An empty string is not the same as null.

If the target contract says blank strings should be omitted, make that rule explicit:

var phone = payload.phone default ""
---
{
    customerId: payload.customerId,
    (phone: phone) if (!isEmpty(trim(phone)))
}

This also treats whitespace-only input as empty after trimming.

Again, confirm the semantics. An empty string may intentionally mean "clear this text field" in some APIs.

Removing Empty Arrays

Suppose the source sends:

{
  "customerId": "C100",
  "contacts": []
}

If an empty contacts array should not be sent:

{
    customerId: payload.customerId,
    (contacts: payload.contacts) if (!isEmpty(payload.contacts default []))
}

But be careful with update semantics. Some APIs interpret:

"contacts": []

as an instruction to remove all contacts. Omitting contacts may mean leave them unchanged.

Those are completely different business operations.

Removing Empty Objects

The same principle applies to nested objects:

var address = payload.address default {}
---
{
    customerId: payload.customerId,
    (address: address) if (!isEmpty(address))
}

This is useful when the source creates structural placeholders that the target does not need.

Cleaning an Object Dynamically

When you are dealing with a dynamic object rather than a fixed contract, you can filter entries based on their values.

For example, to remove null-valued entries:

%dw 2.0
output application/json
---
payload filterObject ((value, key) -> value != null)

Input:

{
  "name": "Acme",
  "industry": null,
  "country": "US"
}

Output:

{
  "name": "Acme",
  "country": "US"
}

This approach is useful when fields are dynamic or numerous and the same rule applies to all of them.

Should You Remove Every Empty Value?

Usually, no.

Consider this payload:

{
  "nickname": "",
  "contacts": [],
  "preferences": {},
  "terminationDate": null
}

Each value could mean something different:

ValuePossible meaning
""clear a text value, or simply blank input
[]remove all child values, or no values supplied
{}intentionally empty object, or placeholder
nullclear a value, unknown value, or missing information

A generic "remove empties" function can silently destroy these distinctions.

For stable enterprise contracts, explicit field-level rules are often safer than aggressively cleaning every value.

Example: Preparing a Salesforce Record for a Target API

Suppose a Salesforce Account contains several optional fields and the target API expects omitted fields to mean "do not update."

%dw 2.0
output application/json
var phone = payload.Phone default ""
var website = payload.Website default ""
---
{
    accountId: payload.Id,
    name: payload.Name,
    (phone: phone) if (!isEmpty(trim(phone))),
    (website: website) if (!isEmpty(trim(website))),
    (industry: payload.Industry) if (payload.Industry != null)
}

This transformation makes the omission policy visible instead of relying on an undocumented cleanup step.

Choose the Pattern Based on the Requirement

A useful decision guide is:

RequirementPattern
Omit nulls broadly from JSON objectsskipNullOn="objects"
Omit null for selected fields onlyconditional object elements
Remove null entries from a dynamic objectfilterObject
Omit blank stringsexplicit isEmpty / normalization rule
Omit empty arrays or objectsexplicit condition after confirming semantics

Practical Rule

Treat payload cleanup as contract behavior, not formatting.

Before removing a null or empty value, ask:

What would the target system do if I sent this value, and what would it do if I omitted the field entirely?

Once that answer is clear, the DataWeave implementation is usually straightforward.

CONTINUE READING

Explore closely related architecture, integration and implementation topics.