This module contains helper functions for working 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(items: Object, amount: 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 <: Object>(obj: 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.
Parameters
Name | Description |
---|---|
obj |
The |
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: Object, condition: (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 object.
Parameters
Name | Description |
---|---|
object |
The object to evaluate. |
condition |
The condition to apply to each element. |
Example
This example shows how everyEntry
behaves with 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(list: Null, condition: (Nothing, Nothing) → Boolean): Boolean
Helper function that enables everyEntry
to work with a null
value.
keySet
keySet<K, V>(obj: { (K)?: V }): Array<K>
Returns an array of key names from an object.
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 <: Object, V <: Object>(source: T, target: 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 |
The object to which the |
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<T <: Object>(a: Null, b: T): T
Helper function that enables mergeWith
to work with a null
value.
mergeWith<T <: Object>(a: T, b: Null): T
Helper function that enables mergeWith
to work with a null
value.
nameSet
nameSet(obj: Object): Array<String>
Returns an array of keys from an object.
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(obj: Object, condition: (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.
Parameters
Name | Description |
---|---|
obj |
The object to evaluate. |
condition |
The condition to use when evaluating elements in the object. |
Example
This example shows how the someEntry
behaves with 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(obj: Null, condition: (value: Nothing, key: Nothing) → Boolean): Boolean
Helper function that enables someEntry
to work with a null
value.
takeWhile
takeWhile<T>(obj: Object, condition: (value: Any, key: Key) → Boolean): Object
Selects key-value pairs from the object while the condition is met.
Parameters
Name | Description |
---|---|
obj |
The object to filter. |
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>(obj: { (K)?: V }): Array<V>
Returns an array of the values from key-value pairs in an object.
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] }