This module contains helper functions to work with Objects.

To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Objects to the header of your DataWeave script.

Functions

divideBy

divideBy(Object, Number): Array<Object>

Breaks up an object into sub-objects that contain the specified number of key-value pairs.

If there are fewer key-value pairs in an object than the specified number, the function will fill the object with those pairs. If there are more pairs, the function will fill another object with the extra pairs.

Parameters
Name Description

items

Key-value pairs in the source object.

amount

The number of key-value pairs allowed in an object.

Example

This example breaks up objects into sub-objects based on the specified amount.

Source
1
2
3
4
5
%dw 2.0
import divideBy from dw::core::Objects
output application/json
---
{ "divideBy" : {"a": 1, "b" : true, "a" : 2, "b" : false, "c" : 3} divideBy 2 }
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "divideBy": [
    {
      "a": 1,
      "b": true
    },
    {
      "a": 2,
      "b": false
    },
    {
      "c": 3
    }
  ]
}

entrySet

entrySet(T): Array<{| key: Key, value: Any, attributes: Object |}>

Returns an array of key-value pairs that describe the key, value, and any attributes in the input object.

This method has been Deprecated and please use entriesOf from Core module

Parameters
Name Description

obj

The Object to describe.

Example

This example returns the key, value, and attributes in the object specified in the variable myVar.

Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Objects
var myVar = read('<xml attr="x"><a>true</a><b>1</b></xml>', 'application/xml')
output application/json
---
{ "entrySet" : entrySet(myVar) }
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "entrySet": [
    {
       "key": "xml",
       "value": {
         "a": "true",
         "b": "1"
       },
       "attributes": {
         "attr": "x"
       }
    }
  ]
}

everyEntry

everyEntry(Object, (value: Any, key: Key) → Boolean): Boolean

Returns true if every entry in the object matches the condition.

The function stops iterating after the first negative evaluation of an element in the array.

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

Parameters
Name Description

object

The object to be evaluated

condition

The condition that will be verified for every element

Example

This example shows how the everyEntry behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import everyEntry from dw::core::Objects
output application/json
---
{
    a: {} everyEntry (value, key) -> value is String,
    b: {a: "", b: "123"} everyEntry (value, key) -> value is String,
    c: {a: "", b: 123} everyEntry (value, key) -> value is String,
    d: {a: "", b: 123} everyEntry (value, key) -> key as String == "a",
    e: {a: ""} everyEntry (value, key) -> key as String == "a",
    f: null everyEntry ((value, key) -> key as String == "a")
}
Output
1
2
3
4
5
6
7
8
{
  "a": true,
  "b": true,
  "c": false,
  "d": false,
  "e": true,
  "f": true
}

everyEntry(Null, (Nothing, Nothing) → Boolean): Boolean

Helper function that makes everyEntry work with null values

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

keySet

keySet(T): ?

Returns an array of key names from an object.

This method has been Deprecated and please use keysOf from Core module

Parameters
Name Description

object

The object to evaluate.

Example

This example returns the keys from the input object.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Objects
output application/json
---
{ "keySet" : keySet({ "a" : true, "b" : 1}) }
Output
1
{ "keySet" : ["a","b"] }
Example

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

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%dw 2.0
import * from dw::core::Objects
var myVar = read('<users xmlns="http://test.com">
                     <user name="Mariano" lastName="Achaval"/>
                     <user name="Stacey" lastName="Duke"/>
                  </users>', 'application/xml')
output application/json
---
{ keySetExample: flatten([keySet(myVar.users) map $.#,
                          keySet(myVar.users) map $.@])
}
++
{ nameSet: flatten([nameSet(myVar.users) map $.#,
                    nameSet(myVar.users) map $.@])
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "keySet": [
    "http://test.com",
    "http://test.com",
    {
      "name": "Mariano",
      "lastName": "Achaval"
    },
    {
      "name": "Stacey",
      "lastName": "Duke"
    }
  ],
  "nameSet": [
    null,
    null,
    null,
    null
  ]
}

mergeWith

mergeWith(T, V): ?

Appends any key-value pairs from a source object to a target object.

If source and target objects have the same key, the function appends that source object to the target and removes that target object from the output.

Parameters
Name Description

source

The object to append to the target.

target

The object to which the source object is appended.

Example

This example appends the source objects to the target. Notice that "a" : true, is removed from the output, and "a" : false is appended to the target.

Source
1
2
3
4
5
%dw 2.0
import mergeWith from dw::core::Objects
output application/json
---
{ "mergeWith" : { "a" : true, "b" : 1} mergeWith { "a" : false, "c" : "Test"} }
Output
1
2
3
4
5
"mergeWith": {
    "b": 1,
    "a": false,
    "c": "Test"
}

mergeWith(Null, T): T

Helper method to make mergeWith null friendly.

mergeWith(T, Null): T

Helper method to make mergeWith null friendly.

nameSet

nameSet(Object): Array<String>

Returns an array of keys from an object.

This method has been Deprecated and please use namesOf from Core module

Parameters
Name Description

obj

The object to evaluate.

Example

This example returns the keys from the input object.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Objects
output application/json
---
{ "nameSet" : nameSet({ "a" : true, "b" : 1}) }
Output
1
{ "nameSet" : ["a","b"] }

someEntry

someEntry(Object, (value: Any, key: Key) → Boolean): Boolean

Returns true if at least one entry in the object matches the specified condition.

The function stops iterating after the first element that matches the condition is found.

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

Parameters
Name Description

obj

The object to be evaluated

condition

The condition that will be verified for every element

Example

This example shows how the someEntry behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import someEntry from dw::core::Objects
output application/json
---
{
    a: {} someEntry (value, key) -> value is String,
    b: {a: "", b: "123"} someEntry (value, key) -> value is String,
    c: {a: "", b: 123} someEntry (value, key) -> value is String,
    d: {a: "", b: 123} someEntry (value, key) -> key as String == "a",
    e: {a: ""} someEntry (value, key) -> key as String == "b",
    f: null someEntry (value, key) -> key as String == "a"
}
Output
1
2
3
4
5
6
7
8
{
  "a": false,
  "b": true,
  "c": true,
  "d": true,
  "e": false,
  "f": false
}

someEntry(Null, (value: Nothing, key: Nothing) → Boolean): Boolean

Helper function that makes someEntry work with null values

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

takeWhile

takeWhile(Object, (value: Any, key: Key) → Boolean): Object

Selects key-value pairs from the object while the condition is met.

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

Parameters
Name Description

obj

The object to be filtered.

condition

The condition (or expression) used to match a key-value pairs in the object.

Example

This example iterates over the key-value pairs in the object and selects the elements while the condition is met. It outputs the result into an object.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Objects
output application/json
var obj = {
  "a": 1,
  "b": 2,
  "c": 5,
  "d": 1
}
---
obj takeWhile ((value, key) ->  value < 3)
Output
1
2
3
4
{
  "a": 1,
  "b": 2
}

valueSet

valueSet({ (K)?: V }): Array<V>

Returns an array of the values from key-value pairs in an object.

This method has been Deprecated and please use valuesOf from Core module

Parameters
Name Description

obj

The object to evaluate.

Example

This example returns the values from the input object.

Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Objects
output application/json
---
{ "valueSet" : valueSet({a: true, b: 1}) }
Output
1
{ "valueSet" : [true,1] }