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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago