How to check for empty values in an array in DataWeave | Part 1: sizeOf, groupBy, isEmpty, default
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 1: Using sizeOf, groupBy, isEmpty, and default.
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 first solution. Something like this:
%dw 2.0
output application/json
---
["notEmpty", "", null]
Step 1: We’ll use the “groupBy” function to separate the non-empty and empty items from the array (using the “isEmpty” function).
["notEmpty", "", null] groupBy isEmpty($)
This will give us an object with two fields: one “false” and one “true.” The “false” field contains an array with the values that are not empty ([“notEmpty”]), and the “true” field contains an array with the values that are empty ([“”, null]).

Step 2: Since we want to count the number of values that are empty, let’s go ahead and extract the “true” field.
(["notEmpty", "", null] groupBy isEmpty($))."true"

Step 3: To count how many values are in this array ([“”, null]), let’s use the “sizeOf” function.
sizeOf((["notEmpty", "", null] groupBy isEmpty($))."true")

Oh, what’s that green line? Ah…It turns out that DataWeave is assuming that any data type can be returned here. It’s not an error, but it’s a warning. You can get rid of it by explicitly coercing the data inside the “sizeOf” function into Array.
sizeOf((["notEmpty", "", null] groupBy isEmpty($))."true" as Array)
Note: The “sizeOf” function only accepts the Array, Object, Binary, and String data types as arguments.
However, if you send a null value instead of an array, this will immediately fail. You can’t coerce Null to Array.
sizeOf((null groupBy isEmpty($))."true" as Array)