How to check for empty values in an array in DataWeave | Part 4: Arrays Module
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 - This solution had some readability issues, the code was showing a warning, and it didn’t fully work for all of our use cases.
Part 2: Using sizeOf, filter, isEmpty, and default - This solution still had some readability issues, and it still didn’t fully work for all of our use cases.
Part 3: Using isEmpty and filter - This solution worked as expected, but we can do better. ;)
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.
Using the Arrays Module
I’ll skip the step-by-step process that we’ve been doing for the past 3 posts to show you 3 different approaches this time.
Let’s take a look at the function we created in the last post.
%dw 2.0
output application/json
fun containsEmptyValues(arr) = if (isEmpty(arr)) true
else not isEmpty(arr filter isEmpty($))
---
{
nullValue: containsEmptyValues(null),
emptyArray: containsEmptyValues([]),
arrayWithEmptyString: containsEmptyValues([""]),
arrayWithNull: containsEmptyValues([null]),
arrayWithEmptyString2: containsEmptyValues(["1", ""]),
arrayWithNull2: containsEmptyValues(["1", null]),
arrayWithValues: containsEmptyValues(["1", "2"]),
arrayWithEmptyObject: containsEmptyValues([{}]),
arrayWithEmptyObject2: containsEmptyValues(["1",{}]),
arrayWithNonEmptyObject: containsEmptyValues([{a:"b"}])
}

We will be using the “some” function from the Arrays module. To do this, we first need to import the function from the array.
%dw 2.0
output application/json
import some from dw::core::Arrays
fun containsEmptyValues(arr) = if (isEmpty(arr)) true
else not isEmpty(arr filter isEmpty($))
---
Note: You can also import all the functions from a module using “import * from…”
After that, we’ll replace the “else” expression with “arr some isEmpty($)”. This will check if at least one of the items inside the “arr” array is an empty value. If this is the case, it will return a true value.
%dw 2.0
output application/json
import some from dw::core::Arrays
fun containsEmptyValues(arr) = if (isEmpty(arr)) true
else arr some isEmpty($)
---

As you can see, we receive the same expected output. We didn’t remove the first condition from the code because when we try to do “arr some isEmpty($)” when arr is null or when arr is an empty array ([]), the output will be false instead of the true value that we’re expecting.

Using Pattern Matching (match/case)
This alternative uses pattern matching with the match/case statements (sort of like the switch/case Java statements).