DataWeave Object Modules: Examples and Function Definitions
The DataWeave object module contains helper functions for working with objects.
To use this module, you must import it to your DataWeave code. For example, you can add the line import * from dw::core::Objects
to the header of your DataWeave script. In this article, we will cover all the functions with examples and their internal functional definition.
divideBy
Breaks up an object into sub-objects that contain the specified number of key-value pairs.
Example:
%dw 2.0
import divideBy from dw::core::Objects
output application/json
---
{ "a": 1, "b": true, "a": 2, "b": false, "c": 3 } divideBy 2
Output:
[
{
"a": 1,
"b": true
},
{
"a": 2,
"b": false
},
{
"c": 3
}
]
Function Definition
fun divideBy(items: Object, amount: Number): Array<{}> = do {
fun internalDivideBy<T>(items: Object, amount: Number, carry:{} ): Array<{}> =
items match {
case {k:v ~ xs} ->
if(sizeOf(carry) == amount - 1)
[carry ++ {(k):v} ~ internalDivideBy(xs, amount, {})]
else
internalDivideBy(xs, amount, carry ++ {(k):v} )
else ->
if(isEmpty(carry))
[]
else
[carry]
}
---
internalDivideBy(items, amount, {})
}
everyEntry
Returns true
if every entry in the object matches the condition
Example:
%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:
{
"a": true,
"b": true,
"c": false,
"d": false,
"e": true,
"f": true
}
Function Definition:
fun everyEntry(object: Object, condition: (value: Any, key: Key) -> Boolean): Boolean = do {
object match {
case {} -> true
case {k:v ~ tail} ->
if (condition(v,k))
everyEntry(tail, condition)
else
false
}
}
mergeWith
Appends any key-value pairs from a source object to a target object. If the 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.
Example:
%dw 2.0
import mergeWith from dw::core::Objects
output application/json
---
{ "a" : true, "b" : 1} mergeWith { "a" : false, "c" : "Test"}
Output:
{
"b": 1,
"a": false,
"c": "Test"
}
Function Definition:
fun mergeWith<T <: Object,V <: Object>(source: T, target: V): ? =
(source -- keySet(target)) ++ target
someEntry
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.
Example:
%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:
{
"a": false,
"b": true,
"c": true,
"d": true,
"e": false,
"f": false
}
Function Definition:
fun someEntry(obj: Object, condition: (value: Any, key: Key) -> Boolean): Boolean = do {
obj match {
case {} -> false
case {k:v ~ tail} ->
if (condition(v,k))
true
else
someEntry(tail, condition)
}
}
takeWhile
Selects key-value pairs from the object while the condition is met.
Example:
%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:
{
"a": 1,
"b": 2
}
Function Definition:
@Since(version = "2.3.0")
fun takeWhile<T>(obj: Object, condition: (value: Any, key: Key) -> Boolean): Object = do {
obj match {
case {} -> obj
case {k:v ~ tail} ->
if (condition(v,k))
{(k): v ~ takeWhile(tail, condition)}
else
{}
}
}
Deprecated Functions Of Objects Module
The below functions are deprecated from Objects Module and moved to the core library functions.
References: https://docs.mulesoft.com/dataweave/2.4/dw-objects
GitHub Repo: https://github.com/shyamrajprasad/dataweave-fun/tree/master/src/main/dw/core/objects