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