Terry Rise
Terry Rise

Reputation: 11

Mule 4.3 DataWeave Function Remove Empty

How to remove and clean out empty object or array from a complex payload. I tried using recursive function but seems it didn't work. In short, any value in the payload if it has an empty array or object, it should be remove from the output.

Upvotes: 1

Views: 2523

Answers (1)

Ray A
Ray A

Reputation: 447

You can use the following function. It will look for an empty object or array and will be remove it.

%dw 2.0
output application/json
fun removeEmpty(a: Array) = a map removeEmpty($) filter (not isEmpty($))
fun removeEmpty(o: Object) = o mapObject 
     if (isEmpty($)) {} 
     else {($$): removeEmpty($)}
fun removeEmpty(s: String) = s
---
removeEmpty(payload)

It's a recursive function and it will check if it's an empty array or object.

Upvotes: 1

Related Questions