Multi-Value Selector (.*)
.* traverses objects and arrays to select the values of all matching keys and returns matching results in an array.
The multi-value selector (.*) returns an Array containing any value that matches the key. The values returned are dependent on the key that’s passed in. The multi-value selector works on both Arrays and Objects, but in different ways.
Let’s check out Object first:
{
“name”: “Emilia”,
“name”: “Isobel”,
“name”: “Euphemia”,
“name”: “Rose”,
“surname”: “Clarke”
}
%dw 2.0
output json
—
payload.*name
[“Emilia”, “Isobel”, “Euphemia”, “Rose”]
The multi-value selector works on Objects by getting the value for every key that matches. This works great when you’re dealing with data with repeating keys. This might seem a little weird for JSON, but consider a similar example in XML, you will see why the multi-value selector is a very important selector to know.
Let’s see how the multi-value selector works on Arrays:
[
{
“price”: 1,
“price”: 2
},
{
“price”: 3,
“price”: 4
},
{
“price”: 5,
“price”: 6
},
{
“name”: “Starburst”,
“category”: “Candy”
}
]
%dw 2.0
output json
—
payload.*price
[1, 2, 3, 4, 5, 6]
In this case, the multi-value selector will go through each item that is an Object and apply the key as if it were a multi-value selector. It’s like DW is evaluating payload[0].*price, then payload[1].*price, etc., and collecting those values into an Array.
.* returns an array with all the values whose key matches the expression.
The following example returns the values of all user elements from the input payload.
Input XML Payload:
<users>
<user>Mariano</user>
<user>Martin</user>
<user>Leandro</user>
</users>
DataWeave Script:
%dw 2.0
output application/json
—
payload.users.*user
Output JSON:
[ “Mariano”, “Martin”, “Leandro” ]
Multi-Value Selector Over an Array
On arrays, .* works the same way as the single-value selector (.). For example, payload.people.person.address.*street and the example payload.people.person.address.street (from Single-Value Selector Over an Array) return the same results.
DataWeave Script:
%dw 2.0
var myArrayOfKeyValuePairs = [ “aString”: “hello”, “aNum”: 2, “aString” : “world” ]
var myArrayOfObjects = [ { “aString”: “hello” }, { “aNum”: 2 }, { “aString” : “world” } ]
output application/json
—
{
myKeyValueExample : myArrayOfKeyValuePairs.*aString,
myObjectExample : myArrayOfObjects.*aString
}
Output JSON:
{
“myKeyValueExample”: [ “hello”, “world” ],
“myObjectExample”: [ “hello”, “world” ]
}
If you like my post please follow me to read my latest post on programming and technology.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…