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 |
---|---|
|
Key-value pairs in the source object. |
|
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)
Returns a list of key-value pairs that describe the key, value, and any attributes in the input object.
Parameters
Name | Description |
---|---|
|
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"
}
}
]
}
keySet
keySet(T): ?
Returns the list of key names from an object.
Parameters
Name | Description |
---|---|
|
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 |
---|---|
|
The object to append to the |
|
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(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 a list of keys from an object.
Parameters
Name | Description |
---|---|
|
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"] }
valueSet
valueSet({ (K)?: V }): Array<V>
Returns the list of the values in an object.
Parameters
Name | Description |
---|---|
|
The object to evaluate. |
Example
This example returns the values from the input object.
Source
1
2
3
4
5
%dw 2.0
import dw::core::Objects
output application/json
---
{ "valueSet" : valueSet({a: true, b: 1}) }
Output
1
{ "valueSet" : [true,1] }