Contact Us 1-800-596-4880

keysOf

keysOf<K, V>(obj: { (K)?: V }): Array<K>

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

The returned keys belong to the Key type. To return each key as a string, you can use namesOf, instead.

Introduced in DataWeave version 2.3.0.

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

keysOf(obj: Null): Null

Helper function that enables keysOf to work with a null value.

Introduced in DataWeave version 2.4.0.