Single-Value Selector

Single-Value Selector (.myKey)

.myKey selectors work over an object or array to return the value of a matching key.

The single-value selector (.) allows you to lookup Object values by their key. Here’s an example:


Input:

{

“name”: “Ana”,

“age”: 29

}

DW Script:

%dw 2.0

output json

payload.age

Output:

29


You can also use the single-value selector with square brackets instead of a period. This allows you to do useful things like using a dynamic key:


Input:

{

“name”: “Ana”,

“age”: 29,

“dynamicKey”: “age”

}

DW Script:

%dw 2.0

output json

{

fixed: payload.age,

dynamic: payload[payload.dynamicKey]

}

Output:

{

“fixed”: 29,

“dynamic”: 29

}


If you’re dealing with a series of nested Objects, you can string together single-value selectors to get to the value you need.

Single-Value Selector Over an Object

For an object, the single-value selector returns the value of the matching key. For example, in the following script, myObject.user returns “a”.

DataWeave Script:

%dw 2.0

var myObject = { user : “a” }

output application/json

{ myObjectExample : myObject.user }

Output JSON:

{

“myObjectExample”: “a”

}

When operating on a DataWeave object (not an array), the . selector only returns the value of the first matching key, even if the object contains multiple matching keys, for example:

DataWeave Script:

%dw 2.0

var myObject = { user : “a”, “user” : “b” }

output application/json

{ myObjectExample : myObject.user }

Output JSON:

{

“myObjectExample”: “a”

}

To return the values of multiple matching keys in cases like this, see Multi Value Selector(.*)

In the next example, payload.people.person.address returns the value of the address element. (It also uses the output directive to transform the JSON input to XML.)

DataWeave Script:

%dw 2.0

var myData = {

“people”: {

“size” : 1,

“person”: {

“name”: “Nial”,

“address”: {

“street”: {

“name”: “Italia”,

“number”: 2164

},

“area”: {

“zone”: “San Isidro”,

“name”: “Martinez”

}

}

}

}

}

output application/xml

{ myaddresses: myData.people.person.address }

Output XML:

<?xml version=”1.0″ encoding=”UTF-8″?>

<myaddresses>

<street>

<name>Italia</name>

<number>2164</number>

</street>

<area>

<zone>San Isidro</zone>

<name>Martinez</name>

</area>

</myaddresses>

Single-Value Selector Over an Array

When acting on an array, the . selector returns an array, even if there is only one matching value. For example, [“a”:”b”].”a” returns [“b”].

Note that the . selector acts differently on arrays than it acts on objects. Like .*, the . selector returns an array with the values of all matching keys at the specified level of the input array.

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

}

In the following example, the value of the input variable, myData, is an array that contains two objects. The selector navigates both objects and returns the values of both street keys.

DataWeave Script:

%dw 2.0

var myData = {

“people”: [

{

“person”: {

“name”: “Nial”,

“address”: {

“street”: {

“name”: “Italia”,

“number”: 2164

},

“area”: {

“zone”: “San Isidro”,

“name”: “Martinez”

}

}

}

},

{

“person”: {

“name”: “Coty”,

“address”: {

“street”: {

“name”: “Monroe”,

“number”: 323

},

“area”: {

“zone”: “BA”,

“name”: “Belgrano”

}

}

}

}

]

}

output application/json

myData.people.person.address.street

Output JSON:

[

{

“name”: “Italia”,

“number”: 2164

},

{

“name”: “Monroe”,

“number”: 323

}

]

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