This module contains core DataWeave functions for data transformations. It is automatically imported into any DataWeave script.
Functions
++
++(Array<S>, Array<T>): Array<S | T>
Concatenates the elements of two lists (arrays) into a new list.
If the two arrays contain different types of elements, the resulting array
is all of S
type elements of Array<S>
followed by all the T
type elements
of Array<T>
. Either of the arrays can also have mixed-type elements. Note
that the arrays can contain any supported data type.
Parameters
Name | Description |
---|---|
|
The source list (an `Array). |
|
The list to concatenate with the source list. |
Example
The example concatenates an Array<Number>
with an Array<String>
.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"result" : [0, 1, 2] ++ ["a", "b", "c"]
}
Output
1
2
3
{
"result": [0, 1, 2, "a", "b", "c"]
}
Example
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"a" : [0, 1, true, "my string"] ++ [2, [3,4,5], {"a": 6}]
}
Output
1
2
3
{
"a": [0, 1, true, "my string", 2, [3, 4, 5], { "a": 6}]
}
++(String, String): String
Concatenates the characters of two strings.
Strings are treated as arrays of characters, so the ++
operator concatenates
the characters of each String as if they were arrays of single character
String.
Parameters
Name | Description |
---|---|
|
The source string. |
|
The string to concatenate with the source string. |
Example
In the example, the Mule
is treated as Array<String> ["M", "u", "l", "e"]
.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : "Mule" ++ "Soft"
}
Output
1
2
3
{
"name": MuleSoft
}
++(Object, Object): Object
Concatenates two objects and returns one flattened object.
The ++
operator extracts all the key-values pairs from each object,
then combines them together into one result object.
Parameters
Name | Description |
---|---|
|
The source object. |
|
The object to concatenate with the source object. |
Example
This example concatenates two objects and transforms them to XML.
Source
1
2
3
4
%dw 2.0
output application/xml
---
"concat" : {aa: "a", bb: "b"} ++ {cc: "c"}
Output
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<concat>
<aa>a</aa>
<bb>b</bb>
<cc>c</cc>
</concat>
++(Date, LocalTime): LocalDateTime
Appends a LocalTime
with a Date
to return a LocalDateTime
value.
Date
and LocalTime
instances are written in standard Java notation,
surrounded by pipe (|
) symbols. The result is a LocalDateTime
object
in the standard Java format. Note that the order in which the two objects are
concatenated is irrelevant, so logically, Date LocalTime` produces the
same result as `LocalTime Date
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates a Date
and LocalTime
object to return a
LocalDateTime
.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"LocalDateTime" : (|2017-10-01| ++ |23:57:59|)
}
Output
1
2
3
{
"LocalDateTime": "2017-10-01T23:57:59"
}
++(LocalTime, Date): LocalDateTime
Appends a LocalTime
with a Date
to return a LocalDateTime
.
Note that the order in which the two objects are concatenated is irrelevant,
so logically, LocalTime Date` produces the same result as
`Date LocalTime
.
Example
This example concatenates LocalTime
and Date
objects to return a
LocalDateTime
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"LocalDateTime" : (|23:57:59| ++ |2003-10-01|)
}
Output
1
2
3
{
"LocalDateTime": "2017-10-01T23:57:59"
}
++(Date, Time): DateTime
Appends a Date
to a Time
in order to return a DateTime
.
Note that the order in which the two objects are concatenated is irrelevant,
so logically, Date
+ Time
produces the same result as Time
+ Date
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates Date
and Time
objects to return a
DateTime
.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"DateTime" : |2017-10-01| ++ |23:57:59-03:00|,
"DateTime2" : |2017-10-01| ++ |23:57:59Z|
}
Output
1
2
3
4
{
"DateTime": "2017-10-01T23:57:59-03:00",
"DateTime2": "2017-10-01T23:57:59Z"
}
++(Time, Date): DateTime
Appends a Date
to a Time
object to return a DateTime
.
Note that the order in which the two objects are concatenated is irrelevant,
so logically, Date
+ Time
produces the same result as a Time
+ Date
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates Time
and Date
objects to return DateTime
objects. Note that the first LocalTime
object is coerced to a `Time
.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"DateTime1" : (|23:57:59| as Time) ++ |2017-10-01|,
"DateTime2" : |23:57:59Z| ++ |2017-10-01|,
"DateTime3" : |23:57:59+02:00| ++ |2017-10-01|
}
Output
1
2
3
4
5
{
"DateTime1": "2017-10-01T23:57:59Z",
"DateTime2": "2017-10-01T23:57:59Z",
"DateTime3": "2017-10-01T23:57:59+02:00"
}
++(Date, TimeZone): DateTime
Appends a TimeZone
to a Date
type value and returns a DateTime
result.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates Date
and TimeZone
(-03:00
) to return a
DateTime
. Note the local time in the DateTime
is 00:00:00
(midnight).
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : (|2017-10-01| ++ |-03:00|) }
Output
1
2
3
{
"DateTime": "2017-10-01T00:00:00-03:00"
}
++(TimeZone, Date): DateTime
Appends a Date
to a TimeZone
in order to return a DateTime
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates TimeZone
(-03:00
) and Date
to return a
DateTime
. Note the local time in the DateTime
is 00:00:00
(midnight).
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : |-03:00| ++ |2017-10-01| }
Output
1
2
3
{
"DateTime": "2017-10-01T00:00:00-03:00"
}
++(LocalDateTime, TimeZone): DateTime
Appends a TimeZone
to a LocalDateTime
in order to return a DateTime
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates LocalDateTime
and TimeZone
(-03:00
) to return a
DateTime
.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : (|2003-10-01T23:57:59| ++ |-03:00|) }
Output
1
2
3
{
"DateTime": "2003-10-01T23:57:59-03:00"
}
++(TimeZone, LocalDateTime): DateTime
Appends a LocalDateTime
to a TimeZone
in order to return a DateTime
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates TimeZone
(-03:00
) and LocalDateTime
to return a
DateTime
.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "TimeZone" : (|-03:00| ++ |2003-10-01T23:57:59|) }
Output
1
2
3
{
"TimeZone": "2003-10-01T23:57:59-03:00"
}
++(LocalTime, TimeZone): Time
Appends a TimeZone
to a LocalTime
in order to return a Time
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates LocalTime
and TimeZone
(-03:00
) to return a
Time
. Note that the output returns`:00` for the unspecified seconds.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "Time" : (|23:57| ++ |-03:00|) }
Output
1
2
3
{
"Time": "23:57:00-03:00"
}
++(TimeZone, LocalTime): Time
Appends a LocalTime
to a TimeZone
in order to return a Time
.
Parameters
Name | Description |
---|---|
|
A |
|
A |
Example
This example concatenates TimeZone
(-03:00
) and LocalTime
to return a
Time
. Note that the output returns`:00` for the unspecified seconds.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "Time" : (|-03:00| ++ |23:57|) }
Output
1
2
3
{
"Time": "23:57:00-03:00"
}
—
--(Array<S>, Array<Any>): Array<S>
Removes specified items from a list (an array).
Name | Description |
---|---|
|
The list (an |
|
Items to remove from the list. |
Example
This example removes a items from a list.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "a" : [0, 1, 1, 2] -- [1,2] }
Output
1
2
3
{
"a": [0]
}
--({ (K)?: V }, Object): { (K)?: V }
Removes specified key-value pairs from an object.
Parameters
Name | Description |
---|---|
|
The object. |
|
Objects to remove from the source object. |
Example
This example removes a key-value pair from the source object.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"hello" : "world",
"name" : "DW"
} -- { "hello" : "world"}
Output
1
2
3
{
"name": "DW"
}
--(Object, Array<String>)
Removes specified key-value pairs from an object.
Parameters
Name | Description |
---|---|
|
The source object (an |
|
Keys for the key-value pairs to remove from the source object. |
Example
This example removes two key-value pairs from the source object.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"yes" : "no",
"good" : "bad",
"old" : "new"
} -- ["yes", "old"]
Output
1
2
3
{
"good": "bad"
}
--(Object, Array<Key>)
Removes specified key-value pairs from an object.
Parameters
Name | Description |
---|---|
|
The source object (an |
|
A keys for the key-value pairs to remove from the source object. |
Example
This example specifies the key-value pair to remove from the source object.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"hello" : "world",
"name" : "DW"
} -- ["hello" as Key]
Output
1
2
3
{
"name": "DW"
}
abs
abs(Number): Number
Returns the absolute value of an input number.
Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
Example
This example returns the absolute value of the input numbers.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---
{
a: abs(-2),
b: abs(2.5),
c: abs(-3.4),
d: abs(3)
}
Output
1
2
3
4
5
6
{
"a": 2,
"b": 2.5,
"c": 3.4,
"d": 3
}
avg
avg(Array<Number>): Number
Returns the average of numeric values in a list (an array).
A list that is empty or that contains a non-numeric value results in an error.
Example
This example returns the average of multiple arrays.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
a: avg([1, 1000]),
b: avg([1, 2, 3])
}
Output
1
2
3
4
{
"a": 500.5,
"b": 2.0
}
ceil
ceil(Number): Number
Rounds an input number up to the nearest whole number.
Parameters
Name | Description |
---|---|
|
The number to round. |
Example
This example rounds numbers up to the nearest whole numbers. Notice that 2.1
rounds up to 3
.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---
{
a: ceil(1.5),
b: ceil(2.1),
c: ceil(3)
}
Output
1
2
3
4
5
{
"a": 2,
"b": 3,
"c": 3
}
contains
contains(Array<T>, Any): Boolean
Returns true
if a list (array) contains a given value, false
if not.
Parameters
Name | Description |
---|---|
|
The input list (an |
|
An element to find in the list. Can be any supported data type. |
Example
This example indicates whether the input list contains '"3"'.
Source
1
2
3
4
%dw 2.0
output application/json
---
ContainsRequestedItem: payload.root.*order.*items contains "3"
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<root>
<order>
<items>155</items>
</order>
<order>
<items>30</items>
</order>
<order>
<items>15</items>
</order>
<order>
<items>5</items>
</order>
<order>
<items>4</items>
<items>7</items>
</order>
<order>
<items>1</items>
<items>3</items>
</order>
<order>
null
</order>
</root>
Output
1
2
3
{
"ContainsRequestedItem": true
}
contains(String, String): Boolean
Indicates whether a string contains a given substring. Returns true
or false
.
Parameters
Name | Description |
---|---|
|
An input string (a |
|
The substring (a |
Example
This example finds the substring "me"
in "some string"
, so it returns true
.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "ContainsString" : payload.root.mystring contains "me" }
Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>some string</mystring>
</root>
Output
1
{ "ContainsString": true }
contains(String, Regex): Boolean
Returns true
if a string contains a match to a regular expression, false
if not.
Parameters
Name | Description |
---|---|
|
An input string ( |
|
A regular expression for matching characters in the input |
Example
This example finds a match to /s[t|p]rin/
within "A very long string"
,
so it returns true
.
Source
1
2
3
4
%dw 2.0
output application/json
---
ContainsString: payload.root.mystring contains /s[t|p]rin/
Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>A very long string</mystring>
</root>
Output
1
2
3
{
"ContainsString": true
}
daysBetween
daysBetween(Date, Date): Number
Returns the number of days between two dates.
Parameters
Name | Description |
---|---|
|
From date (a |
|
To date (a |
Example
This example returns the number of days between the specified dates.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"days" : daysBetween("2016-10-01T23:57:59-03:00",
"2017-10-01T23:57:59-03:00")
}
Output
1
2
3
{
"days" : 365
}
distinctBy
distinctBy(Array<T>, (item: T, index: Number) → Any): Array<T>
Returns unique values from a list (array) that might have duplicates.
Parameters
Name | Description |
---|---|
|
The list ( |
|
The |
Example
This example removes duplicates of "Kurt Cagle"
from an input array.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
%dw 2.0
output application/json
var record = {
"title": "XQuery Kick Start",
"author": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Kurt Cagle",
"Kurt Cagle",
"Kurt Cagle",
"Vaidyanathan Nagarajan"
],
"year":"2000"
}
---
{
"book" : {
"title" : record.title,
"year" : record.year,
"authors" : record.author distinctBy $
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"book": {
"title": "XQuery Kick Start",
"year": "2000",
"authors": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Vaidyanathan Nagarajan"
]
}
}
distinctBy(Null, (item: Nothing, index: Nothing) → Any): Null
Helper function that allows distinctBy to work with null values.
distinctBy({ (K)?: V }, (value: V, key: K) → Any): Object
Removes duplicate key-value pairs from an Object.
Parameters
Name | Description |
---|---|
|
The object from which to remove the key-value pairs. |
|
The |
Example
This example removes duplicates (<author>James McGovern</author>
)
from <book/>
.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/xml
---
{
book : {
title : payload.book.title,
authors: payload.book.&author distinctBy $
}
}
Input
1
2
3
4
5
6
7
8
<book>
<title> "XQuery Kick Start"</title>
<author>James Linn</author>
<author>Per Bothner</author>
<author>James McGovern</author>
<author>James McGovern</author>
<author>James McGovern</author>
</book>
Output
1
2
3
4
5
6
7
8
<book>
<title> "XQuery Kick Start"</title>
<authors>
<author>James Linn</author>
<author>Per Bothner</author>
<author>James McGovern</author>
</authors>
</book>
endsWith
endsWith(String, String): Boolean
Returns true
if a string ends with a provided substring, false
if not.
Parameters
Name | Description |
---|---|
|
The input string (a |
|
The suffix string to find at the end of the input string. |
Example
This example finds "no" (but not "to") at the end of "Mariano".
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"yes" : "Mariano" endsWith "no",
"no" : "Mariano" endsWith "to"
}
Output
1
2
3
4
{
"yes": true,
"no": false
}
filter
filter(Array<T>, (item: T, index: Number) → Boolean): Array<T>
Filters a list (array) by applying an expression that returns only the matching items from the list.
The expression must return true
or false
. If the expression returns true
for an element, the element remains in the list. If it returns false
for
an element, the element gets filtered out of the results.
Parameters
Name | Description |
---|---|
|
The list (array) to filter. |
|
Function that receives an |
Example
This example returns an array of all elements greater than 2
.
Source
1
[9,2,3,4,5] filter (myitem, myindex) -> (myitem > 2)
Output
1
[9,3,4,5]
Example
This example returns an array of all elements found at an index
greater than 2
.
Source
1
[9,2,3,4,5] filter ((myitem, myindex) -> (myindex > 2))
Output
1
[4,5]
Example
This example returns an array of all elements found at an index ($$
)
greater than 1
where the value of the element is less than 5
.
Source
1
2
3
4
%dw 2.0
output application/json
---
[9, 2, 3, 4, 5] filter (($$ > 1) and ($ < 5))
Output
1
[3,4]
filter(Null, (item: Nothing, index: Nothing) → Boolean): Null
Helper function that allows filter
to work with null values.
filterObject
filterObject({ (K)?: V }, (value: V, key: K, index: Number) → Boolean): { (K)?: V }
Filters an object, keeping the key-value pairs that fulfill the criteria
.
The criteria (a lambda expression) has three parameters: value
, key
, and
index
. You can reference the value with $
, the key with $$
,
and the index with $$$
.
Parameters
Name | Description |
---|---|
|
The input object. |
|
The expression that determines whether to retain the key-value pair or not. |
Example
This example keeps only the key-value pairs if the value equals "a"
.
Source
1
2
3
4
%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b"} filterObject ((value) -> value == "a")
Output
1
2
3
{
"letter1": "a"
}
Example
This example only keeps the key-value pairs where the key starts with "letter".
Source
1
2
3
4
%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b", "id": 1} filterObject ((value, key) -> key startsWith "letter")
filterObject(Null, (value: Nothing, key: Nothing, index: Nothing) → Boolean): Null
Helper function that allows filterObject
to work with null values.
find
find(Array<T>, Any): Array<Number>
Returns indices of the input array (a list) that match a specified elementToFind
.
Parameters
Name | Description |
---|---|
|
An array with elements of any type. |
|
Value to find in the input array. |
Example
This example finds the index of an element in a string array.
Source
1
2
3
4
%dw 2.0
output application/json
---
["Bond", "James", "Bond"] find "Bond"
Output
1
[0,2]
find(String, Regex): Array<Array<Number>>
Returns the indices in the text that match the specified regular expression (regex) followed by the capture groups.
The first element in each resulting sub-array is the index in the text that matches the regex, and the next ones are the capture groups in the regex (if present).
Note: To retrieve parts of the text that match a regex use the scan function.
Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
Example
This example finds the beginning and ending indices of words that contain ea
Source
1
2
3
4
%dw 2.0
output application/json
---
"I heart DataWeave" find /\w*ea\w*(\b)/
Output
1
2
3
[
[2,7], [8,17]
]
find(String, String): Array<Number>
Lists indices where the specified characters of a string are present.
Parameters
Name | Description |
---|---|
|
A source string. |
|
The string to find in the source string. |
Example
This example lists the indices of "a" found in "aabccdbce".
Source
1
2
3
4
%dw 2.0
output application/json
---
"aabccdbce" find "a"
Output
1
[0,1]
flatMap
flatMap(Array<T>, (item: T, index: Number) → Array<R>): Array<R>
Applies a transformation function to each element in a list (array) and then flattens the result.
Instead of returning an array of arrays (as map
does), it returns a
flattened array (see the flatten
function).
Parameters
Name | Description |
---|---|
|
The list ( |
|
The |
Example
This example returns an array containing each index and item in order. Note
that you can also write the example like this:
{ "users" : ["joe", "pete", "matt"] flatMap ([$$ as String, $]) }
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "users" : ["joe", "pete", "matt"] flatMap (item, index) -> ([index as String, item]) }
Output
1
2
3
4
5
6
7
8
9
10
{
"users": [
"0",
"joe",
"1",
"pete",
"2",
"matt"
]
}
flatMap(Null, (item: Nothing, index: Nothing) → Any): Null
Helper function that allows flatMap
to work with null values.
flatten
flatten(Array<Array<T> | Q>): Array<T | Q>
Flattens an array of arrays into a single, simple array.
Example
This example flattens an array of arrays.
Source
1
2
3
4
%dw 2.0
output application/json
---
flatten(payload)
Input
1
2
3
4
5
[
[3,5],
[9,5],
[154,0.3]
]
Output
1
2
3
4
5
6
7
8
[
3,
5,
9,
5,
154,
0.3
]
flatten(Null): Null
Helper function that allows flatten to work with null values.
floor
floor(Number): Number
Rounds an input number down to the nearest whole number.
Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
Example
This example rounds numbers down to the nearest whole numbers. Notice that
1.5
rounds down to 1
.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"a" : floor(1.5),
"b" : floor(2.2),
"c" : floor(3)
}
Output
1
2
3
4
5
{
"a": 1,
"b": 2,
"c": 3
}
groupBy
groupBy(Array<T>, (item: T, index: Number) → R): { ®: Array<T> }
Classifies the elements of a list (array) using the specified criteria
function.
The resulting object will have the grouping criteria as keys.
Parameters
Name | Description |
---|---|
|
The list ( |
|
Function that receives the |
Example
This example groups the elements of an array based on the language field.
Notice that it uses the item.language
selector to specify the criteria.
Source
1
2
3
4
%dw 2.0
output application/json
---
payload groupBy (item) -> item.language
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"name": "Foo",
"language": "Java"
},
{
"name": "Bar",
"language": "Scala"
},
{
"name": "FooBar",
"language": "Java"
}
]
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"Scala": [
{
"name": "Bar",
"language": "Scala"
}
],
"Java": [
{
"name": "Foo",
"language": "Java"
},
{
"name": "FooBar",
"language": "Java"
}
]
}
groupBy({ (K)?: V }, (value: V, key: K) → R): { ®: { (K)?: V } }
Groups elements of an object based on a supplied key.
Parameters
Name | Description |
---|---|
|
The object to group. |
|
The |
Example
The example groups an object based on its value ($$
).
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "a" : "b"} groupBy $
Output
1
2
3
4
5
{
"b": {
"a": "b"
}
}
groupBy(Null, (Nothing, Nothing) → Any): Null
Helper function that allows groupBy to work with null values.
isBlank
isBlank(String | Null): Boolean
Returns true
the given string is empty or completely whitespace, false
if not.
Parameters
Name | Description |
---|---|
|
A string to evaluate. |
Example
This example indicates whether the given values are blank.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"empty" : isBlank(""),
"withSpaces" : isBlank(" "),
"withText" : isBlank(" 1223")
}
Output
1
2
3
4
5
{
"empty": true,
"withSpaces": true,
"withText": false
}
isDecimal
isDecimal(Number): Boolean
Returns true
if the given number contains a decimal, false
if not.
Parameters
Name | Description |
---|---|
|
A number to evaluate. |
Example
This example indicates whether the input number has a decimal.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"decimal" : isDecimal(1.1),
"decimal" : isDecimal(1)
}
Output
1
2
3
4
{
"decimal": true,
"decimal": false
}
isEmpty
isEmpty(Array<Any>): Boolean
Returns true
if the given list (array) is empty, false
if not.
Parameters
Name | Description |
---|---|
|
The list (an array) to evaluate. |
Example
This example indicates whether the input array is empty.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty([]),
"nonEmpty" : isEmpty([1])
}
Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
isEmpty(String): Boolean
Returns true
if the given string is empty, false
if not.
Parameters
Name | Description |
---|---|
|
A string to evaluate. |
Example
This example indicates whether the input strings are empty.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty(""),
"nonEmpty" : isEmpty("DataWeave")
}
Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
isEmpty(Null): Boolean
Returns true
if the input is null
.
Parameters
Name | Description |
---|---|
|
|
Example
This example indicates whether the input is null
.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"null" : isEmpty(null)
}
Output
1
2
3
{
"null": true
}
isEmpty(Object): Boolean
Returns true
if the given object is empty, false
if not.
Parameters
Name | Description |
---|---|
|
The object to evaluate. |
Example
This example indicates whether the input object is empty.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty({}),
"nonEmpty" : isEmpty({name: "DataWeave"})
}
Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
isEven
isEven(Number): Boolean
Returns true
if the given number is even, false
if not.
Parameters
Name | Description |
---|---|
|
A number to evaluate. |
Example
This example indicates whether the input numbers are even.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"isEven" :
[ isEven(0), isEven(1), isEven(2)]
}
Output
1
2
3
4
5
6
7
{
"isEven" : [
true,
false,
true
]
}
isInteger
isInteger(Number): Boolean
Returns true
if the given number is an integer (which lacks decimals),
false
if not.
Parameters
Name | Description |
---|---|
|
The number to evaluate. |
Example
This example indicates whether the input is an integer.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"integer" : isInteger(1.1),
"integer" : isInteger(1)
}
Output
1
2
3
4
{
"integer": false,
"integer": true
}
isLeapYear
isLeapYear(DateTime): Boolean
Returns true
if it receives a DateTime
for a leap year, false
if not.
Parameters
Name | Description |
---|---|
|
The date-time ( |
Example
This example indicates whether the input is a leap year.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---
{
"leapYear" : isLeapYear(|2016-10-01T23:57:59Z|),
"leapYear" : isLeapYear(|2016-10-01T23:57:59-03:00|),
"leapYear": isLeapYear(|2017-10-01T23:57:59Z|),
"leapYear": isLeapYear(|2017-10-01T23:57:59-03:00|)
}
Output
1
2
3
4
5
6
{
"leapYear": true,
"leapYear": true,
"leapYear": false,
"leapYear": false
}
isLeapYear(Date): Boolean
Returns true
if the input Date
is a leap year, 'false' if not.
Parameters
Name | Description |
---|---|
|
The date ( |
Example
This example indicates whether the input is a leap year.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"leapYear" : isLeapYear(|2016-10-01|),
"leapYear": isLeapYear(|2017-10-01|)
}
Output
1
2
3
4
{
"leapYear": true,
"leapYear": false
}
isLeapYear(LocalDateTime): Boolean
Returns true
if the input local date-time is a leap year, 'false' if not.
Parameters
Name | Description |
---|---|
|
A local date-time ( |
Example
This example indicates whether the input is a leap year.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"leapYear" : isLeapYear(|2016-10-01T23:57:59|),
"leapYear": isLeapYear(|2017-10-01T23:57:59|)
}
Output
1
2
3
4
{
"leapYear": true,
"leapYear": false
}
isOdd
isOdd(Number): Boolean
Returns true
if the given number is odd, false
if not.
Parameters
Name | Description |
---|---|
|
A number ( |
Example
This example indicates whether the input numbers are odd.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"isOdd" :
[ isOdd(0), isOdd(1), isOdd(2) ]
}
Output
1
2
3
4
5
6
7
{
"isOdd": [
false,
true,
false
]
}
joinBy
joinBy(Array<Any>, String): String
Merges a list (array) into a single string value and uses the provided string as a separator between each item in the list.
Note that joinBy
performs the opposite task of splitBy
.
Parameters
Name | Description |
---|---|
|
The list (an |
|
A |
Example
This example joins the elements with a hyphen (-
).
Source
1
2
3
4
%dw 2.0
output application/json
---
"hyphenate" : ["a","b","c"] joinBy "-"
Output
1
2
3
{
"hyphenate": "a-b-c"
}
log
log(String, T): T
Logs the specified value with an optional prefix
, then returns the
value unchanged. The function logs the output as a system log.
This function can be used to debug DataWeave scripts until there a proper debugger is incorporated.
Parameters
Name | Description |
---|---|
|
A string that typically describes the log. |
|
The value to log. |
Example
This example produces the output shown below in a Logger (through the Mule
LoggerMessageProcessor
), while the Mule DefaultLoggingService
prints
My Age - 33
in the console output.
Source
1
2
3
4
5
%dw 2.0
output application/xml
var myvar = { "age" : 33 }
---
{ "age": log("My Age", myvar.age) }
Output
1
2
<?xml version="1.0" encoding="UTF-8"?>
<age>33</age>
lower
lower(String): String
Returns the provided string in lowercase characters.
Parameters
Name | Description |
---|---|
|
A string ( |
Example
This example converts uppercase characters to lower-case.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : lower("MULESOFT")
}
Output
1
2
3
{
"name": "mulesoft"
}
lower(Null): Null
Helper function that allows lower to work with null values.
map
map(Array<T>, (item: T, index: Number) → R): Array<R>
Transforms items from the given list (array) into a new list using the specified mapper function.
Parameters
Name | Description |
---|---|
|
The list ( |
|
Function used to transform each item in the list. It receives an |
Example
This example concatenates the index (plus 1) to each value of the array.
Source
1
2
3
4
%dw 2.0
output application/json
---
['joe', 'pete', 'matt'] map ((item, index) -> (index + 1) ++ '_' ++ item)
Output
1
2
3
4
5
[
"1_joe",
"2_pete",
"3_matt"
]
Example
This example creates an object with the array values, using the index (plus 1) as keys.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
(["joe", "pete", "matt"] map (item, index) -> {(index + 1): item})
}
Output
1
2
3
4
5
{
"1": "joe",
"2": "pete",
"3": "matt"
}
Example
If the parameters of the mapper
function are not named, the index can be
referenced with $$
, and the value with $
.
So the previous example can be written like this.
Source
1
2
3
4
%dw 2.0
output application/json
---
["joe", "pete", "matt"] map (($$ + 1) ++ ":" ++ upper($))
Example
This example shows how to turn an array of key-value pairs into an object.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
["joe", "pete", "matt"] map (($$ + 1) ++ ":" ++ upper($))
=== Output
[source,JSON,linenums]
[ "1:JOE", "2:PETE", "3:MATT" ]
map(Null, (item: Nothing, index: Nothing) → Any): Null
Helper function that allows map
to work with null values.
mapObject
mapObject({ (K)?: V }, (value: V, key: K, index: Number) → Object): Object
Transforms each key-value pair of an object using the specified mapper function.
Parameters
Name | Description |
---|---|
|
The object to transform. |
|
Function that receives the |
Example
This example increases each price by 5 and formats the numbers to always include 2 decimals.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/xml
---
{
prices: payload.prices mapObject (value, key) -> {
(key): (value + 5) as Number {format: "##.00"}
}
}
Input
1
2
3
4
5
6
<?xml version='1.0' encoding='UTF-8'?>
<prices>
<basic>9.99</basic>
<premium>53</premium>
<vip>398.99</vip>
</prices>
Output
1
2
3
4
5
6
<?xml version='1.0' encoding='UTF-8'?>
<prices>
<basic>14.99</basic>
<premium>58.00</premium>
<vip>403.99</vip>
</prices>
mapObject(Null, (value: Nothing, key: Nothing, index: Number) → Nothing): Null
Helper function that allows mapObject
to work with null values.
match
match(String, Regex): Array<String>
Uses an regular expression to match string and then separate it into capture groups. Returns the results in a a list (an array).
It can be applied to the result of any evaluated expression and can return any evaluated expression. See Pattern Matching in DataWeave.
Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
Example
In this example, the regular expression describes an email address. It
contains two capture groups: what come before and after the @
. The
result is an array of three elements: the first is the whole email address,
the second matches one of the capture groups, the third matches the other one.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "hello" : "anniepoint@mulesoft.com" match(/([a-z]*)@([a-z]*).com/) }
Output
1
2
3
4
5
6
7
{
"hello": [
"anniepoint@mulesoft.com",
"anniepoint",
"mulesoft"
]
}
matches
matches(String, Regex): Boolean
Checks if the given matcher matches the (whole) text.
For use cases where you need to output or conditionally process the matched value, see Pattern Matching in DataWeave.
Parameters
Name | Description |
---|---|
|
The input string. |
|
A regular expression for matching characters in the string. |
Example
This example indicates whether the regular expression matches the input text.
Note that you can also use the matches(text,matcher)
notation (for example,
matches("admin123", /a.*\d+/)
).
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"match" : "admin123" matches /a.*\d+/,
"nonmatch" : "admin123" matches /^b.+/
}
Output
1
2
3
4
{
"match": true,
"nonmatch": false
}
max
max(Array<T>): T | Null
Returns the highest numeric value in a list (an array).
Returns null if the array is empty and produces an error when non-numeric values are in the array.
Example
This example returns the maximum value of each input array.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
a: max([1, 1000]),
b: max([1, 2, 3]),
d: max([1.5, 2.5, 3.5])
}
Output
1
2
3
4
5
{
"a": 1000,
"b": 3,
"d": 3.5
}
maxBy
maxBy(Array<T>, (item: T) → Comparable): T | Null
Returns the highest value of comparable elements in the given list (an array).
Returns null when the list is empty. Returns an error if the items in the list are not comparable.
Parameters
Name | Description |
---|---|
|
Element in the given array (of type |
Example
This example returns the highest value within objects (key-value pairs) in
an array. Notice that it uses item.a
to access the value of the object. You
can also write the expression in the source like this:
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy $.a
Source
1
2
3
4
%dw 2.0
output application/json
---
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy ((item) -> item.a)
Output
1
{ "a" : 3 }
min
min(Array<T>): T | Null
Returns the lowest numeric value in an array.
Returns null if the array is empty and produces an error when non-numeric values are in the array.
Example
This example returns the lowest numeric value of each input array.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
a: min([1, 1000]),
b: min([1, 2, 3]),
d: min([1.5, 2.5, 3.5])
}
Output
1
2
3
4
5
{
"a": 1,
"b": 1,
"d": 1.5
}
minBy
minBy(Array<T>, (item: T) → Comparable): T | Null
Returns the lowest value of comparable elements in a list (an array).
Returns null when list is empty. Returns an error if the items in the array are not comparable.
Parameters
Name | Description |
---|---|
|
Element in the list (of type |
Example
This example returns the lowest value within objects (key-value pairs) in
an array. Notice that it uses item.a
to access the value of the object. You
can also write the expression in the source like this:
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] minBy $
Source
1
2
3
4
%dw 2.0
output application/json
---
[ { "a" : 1 }, { "a" : 2 }, { "a" : 3 } ] minBy (item) -> item.a
Output
1
{ "a" : 1 }
mod
mod(Number, Number): Number
Returns the modulo (the remainder after performing dividing the dividend
by the divisor
).
Parameters
Name | Description |
---|---|
|
The number that serves as the dividend for the operation. |
|
The number that serves as the divisor for the operation. |
Example
This example returns the modulo of the input values. Note that you can also
use the mod(dividend, divisor)
notation (for example, mod(3, 2)
to return
1
).
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"a" : 3 mod 2,
"b" : 4 mod 2,
"c" : 2.2 mod 2
}
Output
1
2
3
4
5
{
"a": 1,
"b": 0,
"c": 0.2
}
native
native(String): Nothing
Internal method used to indicate that a function implementation is not written in DataWeave but in Scala.
now
now(): DateTime
Returns a DateTime
object with the current date and time.
Example
This example shows uses of the now()
function with valid
selectors. It also shows how to get the epoch time with now() as Number
.
See also,
DataWeave Selectors.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
output application/json
---
{
now: now(),
epochTime : now() as Number,
nanoseconds: now().nanoseconds,
milliseconds: now().milliseconds,
seconds: now().seconds,
minutes: now().minutes,
hour: now().hour,
day: now().day,
month: now().month,
year: now().year,
quarter: now().quarter,
dayOfWeek: now().dayOfWeek,
dayOfYear: now().dayOfYear,
offsetSeconds: now().offsetSeconds
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"now": "2018-07-23T22:03:04.829Z",
"epochTime": 1533143270,
"nanoseconds": 829000000,
"milliseconds": 829,
"seconds": 4,
"minutes": 3,
"hour": 22,
"day": 23,
"month": 7,
"year": 2018,
"quarter": 3,
"dayOfWeek": 1,
"dayOfYear": 204,
"offsetSeconds": 0
}
orderBy
orderBy(O, (value: V, key: K) → R): O
Reorders the content of an object using a value returned by a function as the criteria.
Note that you can reference the index with
$$
and the value with $
.
Parameters
Name | Description |
---|---|
|
The object to reorder. |
|
The result of the function is used as the criteria to reorder the object. |
Example
This example alphabetically orders the values of each object in the array.
Note that orderBy($.letter)
produces the same result as orderBy($[0])
.
Source
1
2
3
4
%dw 2.0
output application/json
---
orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy($.letter)
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"orderByLetter": [
{
"letter": "a"
},
{
"letter": "b"
},
{
"letter": "c"
},
{
"letter": "d"
},
{
"letter": "e"
}
]
}
Example
The orderBy
function does not have an option to order in descending order
instead of ascending. In these cases, you can simply invert the order of
the resulting array using -
, for example:
Source
1
2
3
4
%dw 2.0
output application/json
---
orderDescending: ([3,8,1] orderBy -$)
Output
1
{ "orderDescending": [8,3,1] }
orderBy(Array<T>, (item: T, index: Number) → R): Array<T>
Sorts an array using the specified criteria.
Parameters
Name | Description |
---|---|
|
The list (an array) to sort. |
|
The result of the function will be used as the criteria to sort the list. It should return a simple value (String, Number, etc) |
Example
This example sorts an array of numbers based on the numeric values.
Source
1
2
3
4
%dw 2.0
output application/json
---
[3,2,3] orderBy $
Output
1
2
3
4
5
[
2,
3,
3
]
Example
This example sorts an array of people based on their age.
Source
1
2
3
4
%dw 2.0
output application/json
---
[{name: "Santiago", age: 42},{name: "Leandro", age: 29}, {name: "Mariano", age: 35}] orderBy (person) -> person.age
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
name: "Leandro",
age: 29
},
{
name: "Mariano",
age: 35
},
{
name: "Santiago",
age: 42
}
]
orderBy(Null, (item: Nothing, index: Nothing) → Null): Null
Helper function that allows orderBy to work with null values.
pluck
pluck({ (K)?: V }, (value: V, key: K, index: Number) → R): Array<R>
Useful for mapping an object into an list (array), pluck
iterates over an
object and returns an array of keys, values, or indices in that object.
It is an alternative to mapObject
, which is similar but returns
an object, instead of an array.
Parameters
Name | Description |
---|---|
|
The object to map. |
|
The |
Example
This example uses pluck
to iterate over each element (object) within
<prices/>
and returns arrays of their keys, values, and indices.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
output application/json
var readXml = read("<prices>
<basic>9.99</basic>
<premium>53.00</premium>
<vip>398.99</vip>
</prices>", "application/xml")
---
"result" : {
"keys" : readXml.prices pluck($$),
"values" : readXml.prices pluck($),
"indices" : readXml.prices pluck($$$)
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"result": {
"keys": [
"basic",
"premium",
"vip"
],
"values": [
"9.99",
"53",
"398.99"
],
"indices": [
0,
1,
2
]
}
}
Example
You can also use named keys and values as parameters. For example, the next
transformation example iterates over the prices
input
above and outputs an array with a single element. Note that
payload pluck(payload.prices)
produces the same result as
payload pluck(payload[0])
.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
var readXml = read("<prices>
<basic>9.99</basic>
<premium>53.00</premium>
<vip>398.99</vip>
</prices>", "application/xml")
---
payload pluck(readXml.prices)
Output
1
2
3
4
5
6
7
[
{
"basic": "9.99",
"premium": "53.00",
"vip": "398.99"
}
]
pluck(Null, (value: Nothing, key: Nothing, index: Nothing) → Any): Null
Helper function that allows pluck to work with null values.
pow
pow(Number, Number): Number
Raises the value of a given base
number to the specified power
.
Parameters
Name | Description |
---|---|
|
A number ( |
|
A number ( |
Example
This example raises the value of the base
number to the specified power
.
Note that you can also use the pow(base,power)
notation (for example,
pow(2,3)
to return 8
).
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
a: 2 pow 3,
b: 3 pow 2,
c: 7 pow 3
}
Output
1
2
3
4
5
{
"a": 8,
"b": 9,
"c": 343
}
random
random(): Number
Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0.
Example
This example generates a pseudo-random number and multiplies it by 1000.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ price: random() * 1000 }
Output
1
{ "price": 65.02770292248383 }
randomInt
randomInt(Number): Number
Returns a pseudo-random whole number from 0 to the specified number (exclusive).
Parameters
Name | Description |
---|---|
|
A number that sets the upper bound of the random number. |
Example
This example returns an integer from 0 to 1000 (exclusive).
Source
1
2
3
4
%dw 2.0
output application/json
---
{ price: randomInt(1000) }
Output
1
{ "price": 442.0 }
read
read(String | Binary, String, Object)
Reads the input string or binary and returns parsed content.
This function can be useful if the reader cannot determine the content type by default.
Parameters
Name | Description |
---|---|
|
The string or binary to read. |
|
A supported format (or content type). Default: |
|
Optional: Sets reader configuration properties. For other formats and reader configuration properties, see Formats Supported by DataWeave. |
Example
This example reads a string as a CSV format without a header and outputs it
to JSON. Notice that is adds column names as keys to the output object. Also,
if you do not append [0]
to the function call, the results will return as an
array (with square brackets surrounding the entire output object).
Source
%dw 2.0 var myVar = "Some, Body" output application/json --- read(myVar,"application/csv",{header:false})[0]
Output
1
2
3
4
{
"column_0": "Some",
"column_1": " Body"
}
Example
This example reads the input XML and shows the syntax for a reader property.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/xml
---
{
"XML" : read("<prices><basic>9.99</basic></prices>",
"application/xml",
{ indexedReader: "false" })."prices"
}
Output
1
2
3
4
5
6
<?xml version='1.0' encoding='UTF-8'?>
<XML>
<basic>9.99</basic>
<premium>53</premium>
<vip>398.99</vip>
</XML>
readUrl
readUrl(String, String, Object)
Similar to the read
function. However, readURL
accepts a URL as input.
Otherwise, it accepts the same arguments as read
.
Parameters
Name | Description |
---|---|
|
The URL string to read. |
|
A supported format (or content type). Default: |
|
Optional: Sets reader configuration properties. For other formats and reader configuration properties, see Formats Supported by DataWeave. |
Example
This example reads JSON object from a URL. (For readability, the output values
shown below are shortened (…
).)
Source
1
2
3
4
%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
Output
1
2
3
4
5
6
{
"userId": 1,
"id": 1,
"title": "sunt aut facere ...",
"body": "quia et suscipit\nsuscipit ..."
}
reduce
reduce(Array<T>, (item: T, accumulator: T) → T): T | Null
Applies the reduction function for each element in the input array.
Note that if the array is empty and no default value is set on the accumulator, a null value is returned.
Parameters
Name | Description |
---|---|
|
Item in the given list. It provides the value to reduce. Can also be referenced as |
|
An accumulator (also referenced as |
Example
This example returns the sum of the values in the input arrays.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"sum" : [0, 1, 2, 3, 4, 5] reduce ($$ + $),
"sum" : [0, 1, 2, 3, 4, 5] reduce ((val, acc) -> acc + val)
}
Output
1
2
3
4
{
"sum": 15,
"sum": 15
}
Example
This example uses the accumulator to concatenate elements in the input arrays and return the results in a string.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"concat" : ["a", "b", "c", "d"] reduce ($$ ++ $),
"concat" : ["a", "b", "c", "d"] reduce ((val, acc) -> acc ++ val)
}
Output
1
2
3
4
{
"concat": "abcd",
"concat": "abcd"
}
Example
This example sets the first elements of the arrays to "z"
and 3
.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"concat" : ["a", "b", "c", "d"] reduce ((item, acc = "z") -> acc ++ item),
"sum": [0, 1, 2, 3, 4, 5] reduce ((val, acc = 3) -> acc + val)
}
Output
1
2
3
4
{
"concat": "zabcd"
"sum": 18
}
Example
This example shows a variety of uses of reduce
, including its application to
arrays of boolean values and objects.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
%dw 2.0
output application/json
var in0 =
{
"a": [0, 1, 2, 3, 4, 5],
"b": ["a", "b", "c", "d", "e"],
"c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }],
"d": [true, false, false, true, true]
}
---
{
"a" : [0, 1, 2, 3, 4, 5] reduce $$,
"b": ["a", "b", "c", "d", "e"] reduce $$,
"c": [{ "letter": "a" }, { "letter": "b" }, { "letter": "c" }] reduce ((val, acc = "z") -> acc ++ val.letter),
"d": [{ letter: "a" }, { letter: "b" }, { letter: "c" }] reduce $$,
"e": [true, false, false, true, true] reduce ($$ and $),
"f": [true, false, false, true, true] reduce ((val, acc) -> acc and val),
"g": [true, false, false, true, true] reduce ((val, acc = false) -> acc and val),
"h": [true, false, false, true, true] reduce $$,
"i": in0.a reduce ($$ + $),
"j": in0.a reduce ((val, acc) -> acc + val),
"k": in0.a reduce ((val, acc = 3) -> acc + val),
"l": in0.a reduce $$,
"m": in0.b reduce ($$ ++ $),
"n": in0.b reduce ((val, acc) -> acc ++ val),
"o": in0.b reduce ((val, acc = "z") -> acc ++ val),
"p": in0.b reduce $$,
"q": in0.c reduce ((val, acc = "z") -> acc ++ val.letter),
"r": in0.c reduce $$,
"s": in0.d reduce ($$ and $),
"t": in0.d reduce ((val, acc) -> acc and val),
"u": in0.d reduce ((val, acc = false) -> acc and val),
"v": in0.d reduce $$,
"w": ([0, 1, 2, 3, 4] reduce ((val, acc = {}) -> acc ++ { a: val })) pluck $,
"x": [] reduce $$,
"y": [] reduce ((val,acc = 0) -> acc + val)
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"a": 0,
"b": "a",
"c": "zabc",
"d": { "letter": "a" },
"e": false,
"f": false,
"g": false,
"h": true,
"i": 15,
"j": 15,
"k": 18,
"l": 0,
"m": "abcde",
"n": "abcde",
"o": "zabcde",
"p": "a",
"q": "zabc",
"r": { "letter": "a" },
"s": false,
"t": false,
"u": false,
"v": true,
"w": [ 0,1,2,3,4 ],
"x": null,
"y": 0
}
reduce(Array<T>, (item: T, accumulator: A) → A): A
replace
replace(String, Regex): ((Array<String>, Number) → String) → String
Replaces the part of a string that matches a regular expression and requires
the use of with
to specify the replacement.
Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
Example
This example replaces the numbers at the end of a string with different
characters. Note that you can also use this notation
replace(text,matcher) with string
(for example,
replace("admin123", /(\d+)/) with("ID")
).
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "replace" : "admin123" replace /(\d+)/ with "ID" }
Output
1
2
3
{
"replace": "adminID"
}
replace(String, String): ((Array<String>, Number) → String) → String
Replaces part of a string with another string.
Parameters
Name | Description |
---|---|
|
A string ( |
|
A string ( |
Example
This example replaces the numbers at the end of a string with different characters.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "replace": "admin123" replace "123" with "ID" }
Output
1
2
3
{
"replace": "adminID"
}
round
round(Number): Number
Rounds an input number up or down to the nearest whole number.
Parameters
Name | Description |
---|---|
|
The number to round. |
Example
This example rounds decimal numbers to the nearest whole numbers.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
a: round(1.2),
b: round(4.6),
c: round(3.5)
}
Output
1
2
3
4
5
{
"a": 1,
"b": 5,
"c": 4
}
scan
scan(String, Regex): Array<Array<String>>
Returns a list (array) with all of the matches found within the given string.
Each match is returned as an array that contains the complete match followed by any capture groups in your regular expression (if present).
Parameters
Name | Description |
---|---|
|
The text to scan ( |
|
A regular expression that describes the pattern to look for in the text. |
Example
In the example, the regex
describes an email address. It contains two
capture groups, the characters before and after the @
. It produces an
an array matching the two email addresses in the input string. Each match
is an array of three elements: The first is the entire email address, and the
second and third are matches to the regex
capture groups.
Source
1
2
3
4
%dw 2.0
output application/json
---
"hello" : "anypt@mulesoft.com,max@mulesoft.com" scan /([a-z]*)@([a-z]*).com/
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"hello": [
[
"anypt@mulesoft.com",
"anypt",
"mulesoft"
],
[
"max@mulesoft.com",
"max",
"mulesoft"
]
]
}
sizeOf
sizeOf(Array<Any>): Number
Returns the number of elements in an array.
Returns 0
if the array is empty.
Example
This example returns a count of elements in the input array.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"arraySize": sizeOf([1,2,3])
}
Output
1
2
3
{
"arraySize": 3
}
sizeOf(Object): Number
Returns the number of key-value pairs in an input object.
Returns 0
if the object is empty.
Example
This example counts the key-value pairs in an input object.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
objectSize: sizeOf({a:1,b:2})
}
Output
1
2
3
{
"objectSize": 2
}
sizeOf(Binary): Number
Returns the byte length of a binary.
Example
This example returns the size of a binary value that is passed through a
variable. The binary is returned by a function in core::Binaries
.
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Binaries
var var1 = fromBase64(000000)
output application/json
---
{ "size" : sizeOf(var1) }
Output
1
2
3
{
"size": 4
}
sizeOf(String): Number
Returns the number of characters (including white space) in an string.
Returns 0
if the string is empty.
Example
This example returns the number of characters in the input strings.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"sizeOfString1" : sizeOf("MuleSoft"),
"sizeOfSting2" : sizeOf("my string")
}
Output
1
2
3
4
{
"sizeOfString1": 8,
"sizeOfSting2": 9
}
splitBy
splitBy(String, Regex): Array<String>
Splits a string based on a regex.
The separator can match any character in the input. Note that splitBy
performs the opposite operation of joinBy
.
Parameters
Name | Description |
---|---|
|
The string to split. |
|
A regular expression used to separate string. If it does not match some part of the string, the function will return the string unseparated in an array. |
Example
This example uses the regular expression \/^*.b./\
to find and use -b-
as
a separator. Notice that the separator is omitted from the output.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy(/^*.b./) }
Output
1
2
3
{
"split": ["a","c"]
}
splitBy(String, String): Array<String>
Splits a string based on a separator.
The separator can match any character in the input. Note that splitBy
performs
the opposite operation of joinBy
.
Parameters
Name | Description |
---|---|
|
The string to split. |
|
A string used to separate the input string. It must match some part of the string. |
Example
This example uses the hyphen (-
) as the separator. Note that the selector is
not retained in the output.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy("-") }
Output
1
2
3
{
"split": ["a", "b", "c"]
}
sqrt
sqrt(Number): Number
Returns the square root of an input number.
Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
Example
This example returns the square root of an input numbers.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"a" : sqrt(4),
"b" : sqrt(25),
"c" : sqrt(100)
}
Output
1
2
3
4
5
{
"a": 2.0,
"b": 5.0,
"c": 10.0
}
startsWith
startsWith(String, String): Boolean
Returns true
or false
depending on whether the input string starts with a
matching prefix.
Parameters
Name | Description |
---|---|
|
The input string. |
|
A string that identifies the prefix. |
Example
This example indicates whether the strings start with a given prefix. Note that
you can also use the startsWith(text,prefix)
notation (for example,
startsWith("Mariano","Mar")
).
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"yes" : "Mariano" startsWith "Mar",
"no" : "Mariano" startsWith "Em"
}
Output
1
2
3
4
{
"yes": true,
"no": false
}
sum
sum(Array<Number>): Number
Returns the sum of numbers in an array.
Returns 0
if the array is empty and produces an error when non-numeric
values are in the array.
Example
This example returns the sum of the values in the input array.
Source
1
2
3
4
%dw 2.0
output application/json
---
sum([1, 2, 3])
Output
1
6
to
to(Number, Number): Range
Returns a range with the specified boundaries.
The upper boundary is inclusive.
Parameters
Name | Description |
---|---|
|
A number ( |
|
A number ( |
Example
This example lists a range of numbers from 1 to 10.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"myRange": 1 to 10
}
Output
1
2
3
{
"myRange": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
trim
trim(String): String
Removes any blank spaces from the beginning and ending of a string.
Parameters
Name | Description |
---|---|
|
The string from which to remove any blank spaces. |
Example
This example trims a string.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"trim": trim(" my long text ")
}
Output
1
2
3
{
"trim": "my long text"
}
trim(Null): Null
Helper function that allows trim to work with null values.
typeOf
typeOf(T): Type<T>
Returns the type of an input value.
Parameters
Name | Description |
---|---|
|
A string, object, array, number, or other supported type. |
Example
This example identifies the type of the input string.
Source
1
2
3
4
%dw 2.0
output application/json
---
typeOf("A Text")
Output
1
"String"
unzip
unzip(Array<Array<T>>): Array<Array<T>>
Performs the opposite of zip
.
Given a single array where each index contains an array with two elements,
unzip
outputs two separate arrays, each with the corresponding elements
of each pair. If the indices in the provided array contain arrays with more
than two elements, the output contains as many arrays as there are
elements for each index.
Example
This example unzips an array of arrays. Note that in example b
, the number
of elements in input array is not consistent. So the function only creates
as many full arrays as it can, in this case just one.
Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"a" : unzip([[0,"a"],[1,"b"],[2,"c"],[3,"d"]]),
"b" : unzip([ [0,"a"], [1,"a","foo"], [2], [3,"a"]])
}
Output
1
2
3
4
5
6
7
8
9
{
"a":[
[0, 1, 2, 3],
["a", "b", "c", "d"]
],
"b": [
[0,1,2,3]
]
}
upper
upper(String): String
Returns the provided string in upper-case characters.
Parameters
Name | Description |
---|---|
|
The string to convert to uppercase. |
Example
This example converts lowercase characters to uppercase.
Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : upper("mulesoft")
}
Output
1
2
3
{
"name": "MULESOFT"
}
upper(Null): Null
Helper function that allows trim to work with null values.
uuid
uuid(): String
Returns a v4 UUID using random numbers as the source.
Example
This example generates a random v4 UUID.
Source
%dw 2.0 output application/json --- uuid()
Output
1
"7cc64d24-f2ad-4d43-8893-fa24a0789a99"
with
with(((V, U) → R) → X, (V, U) → R): X
When used with replace
, with
passes a replacement.
Example
This example replaces all numbers in a string with "x" characters.
Source
1
2
3
4
%dw 2.0
output application/json
---
{ "ssn" : "987-65-4321" replace /[0-9]/ with("x") }
Output
1
{ "ssn": "xxx-xx-xxxx" }
write
write(Any, String, Object): String | Binary
Writes (as a String or Binary) the given value in a specific format.
Returns a string or binary with the serialized representation of the value in the specified mimeType (format). You might use this function when you need some data in a format different from the script output, for example to embed data as JSON/CSV inside an XML.
Parameters
Name | Description |
---|---|
|
The value to write. The value can be of any supported data type. |
|
A supported format (or content type) to write. Default: |
|
Optional: Sets writer configuration properties. For writer configuration properties (and other content types), see Formats Supported by DataWeave. |
Example
This example takes JSON input and writes the payload to a CSV format that uses a
pipe (|
) separator and includes the header (matching keys in the JSON objects).
Note that if you instead use "header":false
in your script, the output will
lack the Name|Email|Id|Title
header in the output.
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"Name": "Mr White",
"Email": "white@mulesoft.com",
"Id": "1234",
"Title": "Chief Java Prophet"
},
{
"Name": "Mr Orange",
"Email": "orange@mulesoft.com",
"Id": "4567",
"Title": "Integration Ninja"
}
]
Source
1
2
3
4
5
6
%dw 2.0
output application/xml
---
{
"output" : write(payload, "application/csv", {"header":true, "separator" : "|"})
}
Output
1
2
3
4
5
<?xml version="1.0" encoding="US-ASCII"?>
<output>Name|Email|Id|Title
Mr White|white@mulesoft.com|1234|Chief Java Prophet
Mr Orange|orange@mulesoft.com|4567|Integration Ninja
</output>
zip
zip(Array<T>, Array<R>): Array<Array<T | R>>
Merges elements of two lists (arrays) into a single list (an array of arrays
in consecutive n
-tuples).
Imagine two input lists each as one side of a zipper. Similar to the
interlocking teeth of a zipper, the zip
function interlocks each element
from each input list, one element at a time.
Parameters
Name | Description |
---|---|
|
The array input on the left-hand side of the function. |
|
The array input on the right-hand side of the function. |
Example
This example interdigitates (zips together) elements of the left-hand and right-hand arrays. Notice that only elements with counterparts at the same index are returned in the array.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---
{
"a" : [0, 1, 2, 3] zip ["a", "b", "c", "d"],
"b" : [0, 1, 2, 3] zip ["a"],
"c" : [0, 1, 2, 3] zip ["a", "b"],
"d" : [0, 1, 2] zip ["a", "b", "c", "d"]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"a": [
[0,"a"],
[1,"b"],
[2,"c"],
[3,"d"]
],
"b": [
[0,"a"]
],
"c": [
[0,"a"],
[1,"b"]
],
"d": [
[0,"a"],
[1,"b"],
[2,"c"]
]
}
Example
This example zips more than two input lists.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
var myvar = {
"list1": ["a", "b", "c", "d"],
"list2": [1, 2, 3],
"list3": ["aa", "bb", "cc", "dd"],
"list4": [["a", "b", "c"], [1, 2, 3, 4], ["aa", "bb", "cc", "dd"]]
}
---
myvar.list1 zip myvar.list2 zip myvar.list3 zip myvar.list4
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[
[
[
["a",1],
"aa"
],
["a","b","c"]
],
[
[
["b",2],
"bb"
],
[1,2,3,4]
],
[
[
["c",3],
"cc"
],
["aa","bb","cc","dd"]
]
]
Types
Any
The top-level type. Any
extends all of the system types, which
means that anything can be assigned to a Any
typed variable.
1
Any
Array
Array type that requires a Type(T)
to represent the elements of the list.
Example: Array<Number>
represents an array of numbers, and Array<Any>
represents an array of any type.
1
Array
Binary
A blob.
1
Binary
Boolean
A Boolean
type of true
or false
.
1
Boolean
CData
XML defines a CData
custom type that extends from String
and is used
to identify a CDATA XML block.
It can be used to tell the writer to wrap the content inside CDATA or to
check if the input string arrives inside a CDATA block. :cdata
inherits
from the type :string
.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/xml
---
{
"users" :
{
"user" : "Mariano" as CData,
"age" : 31 as CData
}
}
Output
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user><![CDATA[Mariano]]></user>
<age><![CDATA[31]]></age>
</users>
1
String {cdata: true}
Comparable
A union type that represents all the types that can be compared to each other.
1
String | Number | Boolean | DateTime | LocalDateTime | Date | LocalTime | Time | TimeZone
Date
A date represented by a year, month, and day. For example: |2018-09-17|
1
Date
DateTime
A Date
and Time
within a TimeZone
. For example: |2018-09-17T22:13:00Z|
1
DateTime
Dictionary
Generic dictionary interface.
1
{ _?: T }
Enum
This type is based on the Enum Java class.
It must always be used with the class
property, specifying the full Java
class name of the class, as shown in the example below.
1
2
3
4
%dw 2.0
output application/java
---
"Male" as Enum {class: "com.acme.GenderEnum"}
1
String {enumeration: true}
Iterator
This type is based on the iterator Java class. The iterator contains a collection and includes methods to iterate through and filter it.
Just like the Java class, Iterator
is designed to be consumed only once. For
example, if you pass it to a
Logger component,
the Logger consumes it, so it becomes unreadable by further elements in the flow.
1
Array {iterator: true}
Key
A key of an Object
.
1
Key
LocalDateTime
A DateTime
in the current TimeZone
. For example: |2018-09-17T22:13:00|
1
LocalDateTime
LocalTime
A Time
in the current TimeZone
. For example: |22:10:18|
1
LocalTime
NaN
java.lang.Float
and java.lang.Double
have special cases for NaN
and Infinit
.
DataWeave does not have these concepts for its number multi-precision nature.
So when it is mapped to DataWeave values, it is wrapped in a Null with a Schema marker.
1
Null {NaN: true}
Namespace
A Namespace
type represented by a URI
and a prefix.
1
Namespace
Nothing
Bottom type. This type can be assigned to all the types.
1
Nothing
Null
A Null type.
1
Null
Number
A number type: Any number, decimal, or integer is represented by the Number` type.
1
Number
Object
Type that represents any object, which is a collection of Key
and value pairs.
1
Object
Period
A period.
1
Period
Range
A Range
type represents a sequence of numbers.
1
Range
Regex
A regex type.
1
Regex
SimpleType
A union type that represents all the simple types.
1
String | Boolean | Number | DateTime | LocalDateTime | Date | LocalTime | Time | TimeZone | Period
String
These are the native types of DataWeave.
They are the only types that allow the ???
definition.
1
String
Time
A time in a specific TimeZone
. For example: |22:10:18Z|
1
Time
TimeZone
A time zone.
1
TimeZone
Type
Represents a type in the DataWeave type system.
1
Type
Uri
A Uri.
1
Uri