Multi-Value Selector (.*)

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:


Input:

{

“name”: “Emilia”,

“name”: “Isobel”,

“name”: “Euphemia”,

“name”: “Rose”,

“surname”: “Clarke”

}

DW Script:

%dw 2.0

output json

payload.*name

Output:

[“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:


Input:

[

{

“price”: 1,

“price”: 2

},

{

“price”: 3,

“price”: 4

},

{

“price”: 5,

“price”: 6

},

{

“name”: “Starburst”,

“category”: “Candy”

}

]

DW Script:

%dw 2.0

output json

payload.*price

Output:

[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.

Multi-Value Selector Over an Object

.* 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” ]

}

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

4 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

4 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

5 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

5 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 months ago