DataWeave dynamic selector

Bharath Kesavan
Another Integration Blog
4 min readDec 10, 2022

The dynamic selector is not used in a regular day to day basis. Let’s explore what this is and how we can use it!

payload[(value)]
Dynamically “weave” your data

Lets start with a sample payload:

{
"id": "A00001",
"fname": "Firstname",
"lname": "Lastname"
}

We know “id” is the key and “A00001” is the value.

Here, the value of “id” can be obtained using payload.id or $.id within map operator. The “.” single value selector is used to extract the value. We are aware beforehand about the KEY “id” used for transformation.

Similarly the multiple value selector “ .* ” returns an array of matching values for the given key. Both the single and multiple value selectors work using the key which we are already aware.

1. Introduction to dynamic selector

Consider the below example where item master data is from any database. The input gives the categoryId which will be changing each time. Thus in the below execution, the “ [( )] ” selector is used to get the VALUE. Among the other ways to do it (nesting map and mapObject), this is a simple and easy way!

%dw 2.0
output application/json
var inp = "category05012022072122"
---
payload[(inp)]

Please note this can be used with static keys also i.e. payload[(“availability”)] will get us an array of values for all “availability”.

When we choose elements within the array like empList[i] its also a case of dynamic selector execution. Dynamic selector will also reduce necessity of iterations.

2. Lets justify its name — dynamic

Consider a scenario where we are required to select the block of data object from input request payload based on a condition. In this example we have a logistics payload with label “type” to differentiate the chosen transportation method. In this case the respective object will be chosen based on the “type”.

Are there other methods to do this? You could store the value into a variable and call it through payload.anyVariableName. It’s possible in this example. Lets check out the next scenario where we dynamically pass dynamic values to dynamic payload!!!

3. Tighten it up

Now let’s see a complex scenario. We have a university staff DB records and we have to come up with staff hierarchy. This can be obtained using the userId and reporter fields present in the record.

Hierarchy:

Lecturer -> Professor -> Department Head -> Director

Step1: 1:1 relationship —

In this example, the userMap will give the user to reporter 1:1 relationship. When creating the hierarchy, we get the respective “userId: reporterId” map. The first image shows the user hierarchy and how its dynamically called in the multi-level reporter hierarchy.

Example- “10001”: “10002”

In this scenario, there isn’t a proper KEY available to be explicitly defined. The KEY is not known beforehand, it will change — dynamic.

hierarchy map

Step2: 3 level hierarchy —

Further we will create multiple levels of hierarchy. Here “r1” is directly obtained from userMap. Case in point “r2” and “r3” values are obtained by passing the $.reporter value dynamically to the same userMap.

%dw 2.0
output application/json

var userMap = payload map {($.userId):($.reporter)} reduce($ ++ $$)

var reporterMap = payload map {
($.userId):{
r1: ($.reporter),
r2: userMap[($.reporter)] default "",
r3: userMap[(userMap[($.reporter)])] default "",
}
}
---
reporterMap

Step3: Full record for given Id —

Let’s create a structure to include the full reporter details instead of IDs only. That’s dynamically selecting dynamically selected data!

Now storing this in variables in and invoking may not be practical.

%dw 2.0
output application/json
var userMap = payload map {($.userId):($.reporter)} reduce($ ++ $$)
var reporterMap = payload map {
($.userId):{
r1: ($.reporter),
r2: userMap[($.reporter)] default "",
r3: userMap[(userMap[($.reporter)])] default "",
}
}

var grouped = payload groupBy ($.userId)
---
payload map {
($.userId):{
USER: grouped[($.userId)][0] ,
R1: grouped[(reporterMap[($.userId)].r1[0])][0],
R2: grouped[(reporterMap[($.userId)].r2[0])][0],
R3:grouped[(reporterMap[($.userId)].r3[0])][0],
}
}

Now that we understood how dynamic selector works, let’s explore how else this can be used.

Output with Key and Value using “&”:

With descendant selector using “..”:

--

--