Skip to content
PlektonLabs

Gary Shanks · 19 December 2019 · 1 min read

Easy way to skip the field(s) which is NULL – MuleSoft 4

Problem:

Sometimes to work with MuleSoft Anypoint Studio, we have payload with fields contain a null value.
For example:
Input Payload:

{

"id": 001,
"firstName": "John",
"lastName": null,
"phoneNumber": null,
"email": "abc@domain.com"

}

You want to keep the fields which have a value. You want to skip the NULL value fields.

Expected Payload:

{

"id": 001,
"firstName": "John",
"email": "abc@domain.com"

}

Solution:

You can do it very easily in ‘Transform Message / Data Weave’, in MuleSoft Anypoint Studio.

%dw 2.0
output application/json skipNullOn="everywhere"
{
"id": payload.'id', 
"firstName": payload.'firstName',
"lastName": payload.'lastName',
"phoneNumber": payload.'phoneNumber',
"email": payload.'email'

}

Just add skipNullOn=”everywhere” after ‘output application/JSON’

Note:

This attribute only works when the output is in JSON or XML. It will not work for output ‘java’.



 

Have Questions or a Different Take?

PlektonLabs

Reach out to our team. We're always open to sharing benchmarks or bouncing ideas around.