How to check for empty values in an array in DataWeave | Part 3: isEmpty, filter
I had this use-case where I had to make sure all the values inside an array were not empty. By “empty,” I’m referring to an empty string (“”), a null value (null), or even an empty array ([]). There were some cases when I would receive a null value instead of an array. The array could only contain strings or nulls, but not objects. However, I’ll be adding tests to check for empty objects as well.
Note: The term “null” refers to a null value, whereas “Null” refers to the data type. The same rule applies to string/String, array/Array, and object/Object. I can have an empty array ([]), which is of the type Array, or a string “abc,” which is of the type String.
In this series of posts, I explain 6 different approaches to achieve (almost) the same output using different DataWeave functions/operators. As we advance through the posts, the functions will become easier to read, and the logic will have fewer flaws.
Part 3: Using isEmpty and filter.
Part 4: Using the Arrays Module, Pattern Matching, and Function Overloading.
I use the DataWeave Playground throughout these articles. You can follow this post to set up a local Docker image (no previous Docker experience is needed), or you can also open a new Mule project and use the Transform Message component.
Note: You can also use the online DataWeave Playground tool from this link, if available.
Building the solution
I’ll create an array to start building and testing this solution:
%dw 2.0
output application/json
---
["notEmpty", "", null]
Step 1: Just as we did in the previous solution, we’ll use the “filter” function, followed by “isEmpty” to retrieve the empty items from the array.
["notEmpty", "", null] filter isEmpty($)

Step 2: This time, we will not be counting the values with “sizeOf.” Instead, let’s use another “isEmpty” function for this filtered array ([“”, null]). This function can let us know if the array contains any value or an empty array ([]).
isEmpty(["notEmpty", "", null] filter isEmpty($))

With this, we know that the original array ([“notEmpty”, “”, null]) indeed contains empty values (because it’s not empty after filtering it).
Step 3: Although our goal was to receive a true value if there were empty values, not a false, right? Not to worry! We can simply negate the whole thing with the “not” operator.
not isEmpty(["notEmpty", "", null] filter isEmpty($))

Quiz: Do the following scripts generate the same output?
1) not true or true
2) ! true or true
The answer will be revealed in the next post...Surprise! - There will be more! :)
And now…*drumroll*...we get to the test that broke our two previous solutions…What if we send a null value instead of an array?
not isEmpty(null filter isEmpty($))

It didn’t break this time! Woohoo! Since we’re no longer using the “sizeOf” function, which only accepts Array, Object, Binary, and String data types (not Null), we don’t have that issue anymore! “isEmpty” is cool with Array, String, Null, and Object.
Look at that! We’re done! We’re already returning a Boolean, so there’s no need to create a condition. Now we just have to make this a function and call it with our test data.