DataWeave — Tip #4

DZone 's Guide to

DataWeave — Tip #4

Below, you can find the syntax for selecting an element of an array by a condition.

· Integration Zone ·
Free Resource

There are times when you need to get one particular element from an array, but not based on the index. This item needs to fulfill some requirements like equality and so on. In order to perform that task selector, "?" was introduced. This is applicable for both DataWeave 1.0 and 2.0.


Below, you can find the syntax for selecting an element of an array by a condition. This operation accepts array as an input and returns array as well. In the square brackets, we specify a condition that item needs to fulfill. First, we use the selector "?" sign, and after that, in parenthesis, we put condition. In order to check something against item property, we can use "$" variable to access the item. If we would like to get the item by isAvailable property, we would write something like this: $.isAvailable.

Image title

The output of such prepared transformation is an array. If the DataWeave engine does not find any value matching specified condition, it will return a null value.


Below, I have prepared an example array containing two addresses. One of them is a living address and the second one is a permanent address. To identify which one it is, I use type property.

"addresses": [
    "type": "PERM",
    "city": "Warsaw",
    "street": "Jerozolimskie",
    "no": 150,
    "postal": "12-233"
    "type": "LIVI",
    "city": "Warsaw",
    "street": "Asfaltowa",
    "no": 111,
    "postal": "99-233"

After the transformation, I would like to have property permanent and/or living property filled. In order to do this, I need to prepare the selector based on the type property. DataWeave transformation has been depicted below:

Image title

As you can see, I gain access to array's item by dollar ($) variable. The reason why type property is within single quotes is because type is a reserved DataWeave keyword. For the given example, I have used match operator as well to map the extracted item without unnecessary repetitions. Else part was necessary as in case address is not provided empty value should be returned.


The selector of items from an array is rarely used, but it can save the day. I think that it nicely works with a match operator. At first, I found this syntax a little bit too confusing. However, after I used it a couple of times, I got used to it.

dataweave, integration

Published at DZone with permission of Patryk Bandurski , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}