Contact Free trial Login

keysOf

keysOf(T): ?

Returns an array of keys from key-value pairs within the input object.

Introduced in DataWeave 2.3.0. Supported by Mule 4.3 and later.

Parameters

Name Description

object

The object to evaluate.

Example

This example returns the keys from the key-value pairs within the input object.

Source

%dw 2.0
output application/json
---
{ "keysOf" : keysOf({ "a" : true, "b" : 1}) }

Output

{ "keysOf" : ["a","b"] }

Example

This example illustrates a difference between keysOf and namesOf. Notice that keysOf retains the attributes (name and lastName) and namespaces (xmlns) from the XML input, while namesOf returns null for them because it does not retain them.

Source

%dw 2.0
var myVar = read('<users xmlns="http://test.com">
                     <user name="Mariano" lastName="Achaval"/>
                     <user name="Stacey" lastName="Duke"/>
                  </users>', 'application/xml')
output application/json
---
{ keysOfExample: flatten([keysOf(myVar.users) map $.#,
                          keysOf(myVar.users) map $.@])
}
++
{ namesOfExample: flatten([namesOf(myVar.users) map $.#,
                    namesOf(myVar.users) map $.@])
}

Output

{
  "keysOfExample": [
    "http://test.com",
    "http://test.com",
    {
      "name": "Mariano",
      "lastName": "Achaval"
    },
    {
      "name": "Stacey",
      "lastName": "Duke"
    }
  ],
  "namesOfExample": [
    null,
    null,
    null,
    null
  ]
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub