coderz.py

Keep Coding Keep Cheering!

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement