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

Leave a Reply

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