1. dw::Core
This module contains core DataWeave functions for data transformations. It is automatically imported into any DataWeave script.
1.1. Functions
1.1.1. ++
++(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.
1.2. Parameters
Name | Description |
---|---|
|
The source list (an `Array). |
|
The list to concatenate with the source list. |
1.3. Example
The example concatenates an Array<Number>
with an Array<String>
.
1.3.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"result" : [0, 1, 2] ++ ["a", "b", "c"]
}
1.3.2. Output
1
2
3
{
"result": [0, 1, 2, "a", "b", "c"]
}
1.4. Example
1.4.1. 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}]
}
1.4.2. 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.
1.5. Parameters
Name | Description |
---|---|
|
The source string. |
|
The string to concatenate with the source string. |
1.6. Example
In the example, the Mule
is treated as Array<String> ["M", "u", "l", "e"]
.
1.6.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : "Mule" ++ "Soft"
}
1.6.2. 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.
1.7. Parameters
Name | Description |
---|---|
|
The source object. |
|
The object to concatenate with the source object. |
1.8. Example
This example concatenates two objects and transforms them to XML.
1.8.1. Source
1
2
3
4
%dw 2.0
output application/xml
---
"concat" : {aa: "a", bb: "b"} ++ {cc: "c"}
1.8.2. 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
.
1.9. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.10. Example
This example concatenates a Date
and LocalTime
object to return a
LocalDateTime
.
1.10.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"LocalDateTime" : (|2017-10-01| ++ |23:57:59|)
}
1.10.2. 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
.
1.11. Example
This example concatenates LocalTime
and Date
objects to return a
LocalDateTime
.
1.12. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.12.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"LocalDateTime" : (|23:57:59| ++ |2003-10-01|)
}
1.12.2. 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
.
1.13. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.14. Example
This example concatenates Date
and Time
objects to return a
DateTime
.
1.14.1. 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|
}
1.14.2. 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
.
1.15. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.16. Example
This example concatenates Time
and Date
objects to return DateTime
objects. Note that the first LocalTime
object is coerced to a `Time
.
1.16.1. 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|
}
1.16.2. 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.
1.17. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.18. 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).
1.18.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : (|2017-10-01| ++ |-03:00|) }
1.18.2. 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
.
1.19. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.20. 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).
1.20.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : |-03:00| ++ |2017-10-01| }
1.20.2. 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
.
1.21. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.22. Example
This example concatenates LocalDateTime
and TimeZone
(-03:00
) to return a
DateTime
.
1.22.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "DateTime" : (|2003-10-01T23:57:59| ++ |-03:00|) }
1.22.2. 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
.
1.23. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.24. Example
This example concatenates TimeZone
(-03:00
) and LocalDateTime
to return a
DateTime
.
1.24.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "TimeZone" : (|-03:00| ++ |2003-10-01T23:57:59|) }
1.24.2. 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
.
1.25. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.26. Example
This example concatenates LocalTime
and TimeZone
(-03:00
) to return a
Time
. Note that the output returns`:00` for the unspecified seconds.
1.26.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "Time" : (|23:57| ++ |-03:00|) }
1.26.2. 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
.
1.27. Parameters
Name | Description |
---|---|
|
A |
|
A |
1.28. Example
This example concatenates TimeZone
(-03:00
) and LocalTime
to return a
Time
. Note that the output returns`:00` for the unspecified seconds.
1.28.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "Time" : (|-03:00| ++ |23:57|) }
1.28.2. Output
1
2
3
{
"Time": "23:57:00-03:00"
}
1.28.3. —
--(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. |
1.29. Example
This example removes a items from a list.
1.29.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "a" : [0, 1, 1, 2] -- [1,2] }
1.29.2. Output
1
2
3
{
"a": [0]
}
--({ (K)?: V }, Object): { (K)?: V }
Removes specified key-value pairs from an object.
1.30. Parameters
Name | Description |
---|---|
|
The object. |
|
Objects to remove from the source object. |
1.31. Example
This example removes a key-value pair from the source object.
1.31.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"hello" : "world",
"name" : "DW"
} -- { "hello" : "world"}
1.31.2. Output
1
2
3
{
"name": "DW"
}
--(Object, Array<String>)
Removes specified key-value pairs from an object.
1.32. Parameters
Name | Description |
---|---|
|
The source object (an |
|
Keys for the key-value pairs to remove from the source object. |
1.33. Example
This example removes two key-value pairs from the source object.
1.33.1. Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"yes" : "no",
"good" : "bad",
"old" : "new"
} -- ["yes", "old"]
1.33.2. Output
1
2
3
{
"good": "bad"
}
--(Object, Array<Key>)
Removes specified key-value pairs from an object.
1.34. Parameters
Name | Description |
---|---|
|
The source object (an |
|
A keys for the key-value pairs to remove from the source object. |
1.35. Example
This example specifies the key-value pair to remove from the source object.
1.35.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"hello" : "world",
"name" : "DW"
} -- ["hello" as Key]
1.35.2. Output
1
2
3
{
"name": "DW"
}
1.35.3. abs
abs(Number): Number
Returns the absolute value of an input number.
1.36. Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
1.37. Example
This example returns the absolute value of the input numbers.
1.37.1. 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)
}
1.37.2. Output
1
2
3
4
5
6
{
"a": 2,
"b": 2.5,
"c": 3.4,
"d": 3
}
1.37.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.
1.38. Example
This example returns the average of multiple arrays.
1.38.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
a: avg([1, 1000]),
b: avg([1, 2, 3])
}
1.38.2. Output
1
2
3
4
{
"a": 500.5,
"b": 2.0
}
1.38.3. ceil
ceil(Number): Number
Rounds an input number up to the nearest whole number.
1.39. Parameters
Name | Description |
---|---|
|
The number to round. |
1.40. Example
This example rounds numbers up to the nearest whole numbers. Notice that 2.1
rounds up to 3
.
1.40.1. 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)
}
1.40.2. Output
1
2
3
4
5
{
"a": 2,
"b": 3,
"c": 3
}
1.40.3. contains
contains(Array<T>, Any): Boolean
Returns true
if a list (array) contains a given value, false
if not.
1.41. Parameters
Name | Description |
---|---|
|
The input list (an |
|
An element to find in the list. Can be any supported data type. |
1.42. Example
This example indicates whether the input list contains '"3"'.
1.42.1. Source
1
2
3
4
%dw 2.0
output application/json
---
ContainsRequestedItem: payload.root.*order.*items contains "3"
1.42.2. 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>
1.42.3. Output
1
2
3
{
"ContainsRequestedItem": true
}
contains(String, String): Boolean
Indicates whether a string contains a given substring. Returns true
or false
.
1.43. Parameters
Name | Description |
---|---|
|
An input string (a |
|
The substring (a |
1.44. Example
This example finds the substring "me"
in "some string"
, so it returns true
.
1.44.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "ContainsString" : payload.root.mystring contains "me" }
1.44.2. Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>some string</mystring>
</root>
1.44.3. Output
1
{ "ContainsString": true }
contains(String, Regex): Boolean
Returns true
if a string contains a match to a regular expression, false
if not.
1.45. Parameters
Name | Description |
---|---|
|
An input string ( |
|
A regular expression for matching characters in the input |
1.46. Example
This example finds a match to /s[t|p]rin/
within "A very long string"
,
so it returns true
.
1.46.1. Source
1
2
3
4
%dw 2.0
output application/json
---
ContainsString: payload.root.mystring contains /s[t|p]rin/
1.46.2. Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>A very long string</mystring>
</root>
1.46.3. Output
1
2
3
{
"ContainsString": true
}
1.46.4. daysBetween
daysBetween(Date, Date): Number
Returns the number of days between two dates.
1.47. Parameters
Name | Description |
---|---|
|
From date (a |
|
To date (a |
1.48. Example
This example returns the number of days between the specified dates.
1.48.1. 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")
}
1.48.2. Output
1
2
3
{
"days" : 365
}
1.48.3. distinctBy
distinctBy(Array<T>, (item: T, index: Number) → Any): Array<T>
Returns unique values from a list (array) that might have duplicates.
1.49. Parameters
Name | Description |
---|---|
|
The list ( |
|
The |
1.50. Example
This example removes duplicates of "Kurt Cagle"
from an input array.
1.50.1. 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 $
}
}
1.50.2. 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.
1.51. Parameters
Name | Description |
---|---|
|
The object from which to remove the key-value pairs. |
|
The |
1.52. Example
This example removes duplicates (<author>James McGovern</author>
)
from <book/>
.
1.52.1. 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 $
}
}
1.52.2. 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>
1.52.3. 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>
1.52.4. endsWith
endsWith(String, String): Boolean
Returns true
if a string ends with a provided substring, false
if not.
1.53. Parameters
Name | Description |
---|---|
|
The input string (a |
|
The suffix string to find at the end of the input string. |
1.54. Example
This example finds "no" (but not "to") at the end of "Mariano".
1.54.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"yes" : "Mariano" endsWith "no",
"no" : "Mariano" endsWith "to"
}
1.54.2. Output
1
2
3
4
{
"yes": true,
"no": false
}
1.54.3. 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.
1.55. Parameters
Name | Description |
---|---|
|
The list (array) to filter. |
|
Function that receives an |
1.56. Example
This example returns an array of all elements greater than 2
.
1.56.1. Source
1
[9,2,3,4,5] filter (myitem, myindex) -> (myitem > 2)
1.56.2. Output
1
[9,3,4,5]
1.57. Example
This example returns an array of all elements found at an index
greater than 2
.
1.57.1. Source
1
[9,2,3,4,5] filter ((myitem, myindex) -> (myindex > 2))
1.57.2. Output
1
[4,5]
1.58. 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
.
1.58.1. Source
1
2
3
4
%dw 2.0
output application/json
---
[9, 2, 3, 4, 5] filter (($$ > 1) and ($ < 5))
1.58.2. Output
1
[3,4]
filter(Null, (item: Nothing, index: Nothing) → Boolean): Null
Helper function that allows filter
to work with null values.
1.58.3. 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 $$$
.
1.59. Parameters
Name | Description |
---|---|
|
The input object. |
|
The expression that determines whether to retain the key-value pair or not. |
1.60. Example
This example keeps only the key-value pairs if the value equals "a"
.
1.60.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b"} filterObject ((value) -> value == "a")
1.60.2. Output
1
2
3
{
"letter1": "a"
}
1.61. Example
This example only keeps the key-value pairs where the key starts with "letter".
1.61.1. 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.
1.61.2. find
find(Array<T>, Any): Array<Number>
Returns indices of the input array (a list) that match a specified elementToFind
.
1.62. Parameters
Name | Description |
---|---|
|
An array with elements of any type. |
|
Value to find in the input array. |
1.63. Example
This example finds the index of an element in a string array.
1.63.1. Source
1
2
3
4
%dw 2.0
output application/json
---
["Bond", "James", "Bond"] find "Bond"
1.63.2. 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.
1.64. Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
1.65. Example
This example finds the beginning and ending indices of words that contain ea
1.65.1. Source
1
2
3
4
%dw 2.0
output application/json
---
"I heart DataWeave" find /\w*ea\w*(\b)/
1.65.2. Output
1
2
3
[
[2,7], [8,17]
]
find(String, String): Array<Number>
Lists indices where the specified characters of a string are present.
1.66. Parameters
Name | Description |
---|---|
|
A source string. |
|
The string to find in the source string. |
1.67. Example
This example lists the indices of "a" found in "aabccdbce".
1.67.1. Source
1
2
3
4
%dw 2.0
output application/json
---
"aabccdbce" find "a"
1.67.2. Output
1
[0,1]
1.67.3. 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).
1.68. Parameters
Name | Description |
---|---|
|
The list ( |
|
The |
1.69. 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, $]) }
1.69.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "users" : ["joe", "pete", "matt"] flatMap (item, index) -> ([index as String, item]) }
1.69.2. 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.
1.69.3. flatten
flatten(Array<Array<T> | Q>): Array<T | Q>
Flattens an array of arrays into a single, simple array.
1.70. Example
This example flattens an array of arrays.
1.70.1. Source
1
2
3
4
%dw 2.0
output application/json
---
flatten(payload)
1.70.2. Input
1
2
3
4
5
[
[3,5],
[9,5],
[154,0.3]
]
1.70.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.
1.70.4. floor
floor(Number): Number
Rounds an input number down to the nearest whole number.
1.71. Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
1.72. Example
This example rounds numbers down to the nearest whole numbers. Notice that
1.5
rounds down to 1
.
1.72.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)
}
1.72.2. Output
1
2
3
4
5
{
"a": 1,
"b": 2,
"c": 3
}
1.72.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.
1.73. Parameters
Name | Description |
---|---|
|
The list ( |
|
Function that receives the |
1.74. 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.
1.74.1. Source
1
2
3
4
%dw 2.0
output application/json
---
payload groupBy (item) -> item.language
1.74.2. 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"
}
]
1.74.3. 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): { ®: Array<T> }
Groups elements of an object based on a supplied key.
1.75. Parameters
Name | Description |
---|---|
|
The object to group. |
|
The |
1.76. Example
The example groups an object based on its value ($$
).
1.76.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "a" : "b"} groupBy $
1.76.2. 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.
1.76.3. isBlank
isBlank(String | Null): Boolean
Returns true
the given string is empty or completely whitespace, false
if not.
1.77. Parameters
Name | Description |
---|---|
|
A string to evaluate. |
1.78. Example
This example indicates whether the given values are blank.
1.78.1. Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"empty" : isBlank(""),
"withSpaces" : isBlank(" "),
"withText" : isBlank(" 1223")
}
1.78.2. Output
1
2
3
4
5
{
"empty": true,
"withSpaces": true,
"withText": false
}
1.78.3. isDecimal
isDecimal(Number): Boolean
Returns true
if the given number contains a decimal, false
if not.
1.79. Parameters
Name | Description |
---|---|
|
A number to evaluate. |
1.80. Example
This example indicates whether the input number has a decimal.
1.80.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"decimal" : isDecimal(1.1),
"decimal" : isDecimal(1)
}
1.80.2. Output
1
2
3
4
{
"decimal": true,
"decimal": false
}
1.80.3. isEmpty
isEmpty(Array<Any>): Boolean
Returns true
if the given list (array) is empty, false
if not.
1.81. Parameters
Name | Description |
---|---|
|
The list (an array) to evaluate. |
1.82. Example
This example indicates whether the input array is empty.
1.82.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty([]),
"nonEmpty" : isEmpty([1])
}
1.82.2. Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
isEmpty(String): Boolean
Returns true
if the given string is empty, false
if not.
1.83. Parameters
Name | Description |
---|---|
|
A string to evaluate. |
1.84. Example
This example indicates whether the input strings are empty.
1.84.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty(""),
"nonEmpty" : isEmpty("DataWeave")
}
1.84.2. Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
isEmpty(Null): Boolean
Returns true
if the input is null
.
1.85. Parameters
Name | Description |
---|---|
|
|
1.86. Example
This example indicates whether the input is null
.
1.86.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"null" : isEmpty(null)
}
1.86.2. Output
1
2
3
{
"null": true
}
isEmpty(Object): Boolean
Returns true
if the given object is empty, false
if not.
1.87. Parameters
Name | Description |
---|---|
|
The object to evaluate. |
1.88. Example
This example indicates whether the input object is empty.
1.88.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"empty" : isEmpty({}),
"nonEmpty" : isEmpty({name: "DataWeave"})
}
1.88.2. Output
1
2
3
4
{
"empty": true,
"nonEmpty": false
}
1.88.3. isEven
isEven(Number): Boolean
Returns true
if the given number is even, false
if not.
1.89. Parameters
Name | Description |
---|---|
|
A number to evaluate. |
1.90. Example
This example indicates whether the input numbers are even.
1.90.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"isEven" :
[ isEven(0), isEven(1), isEven(2)]
}
1.90.2. Output
1
2
3
4
5
6
7
{
"isEven" : [
true,
false,
true
]
}
1.90.3. isInteger
isInteger(Number): Boolean
Returns true
if the given number is an integer (which lacks decimals),
false
if not.
1.91. Parameters
Name | Description |
---|---|
|
The number to evaluate. |
1.92. Example
This example indicates whether the input is an integer.
1.92.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"integer" : isInteger(1.1),
"integer" : isInteger(1)
}
1.92.2. Output
1
2
3
4
{
"integer": false,
"integer": true
}
1.92.3. isLeapYear
isLeapYear(DateTime): Boolean
Returns true
if it receives a DateTime
for a leap year, false
if not.
1.93. Parameters
Name | Description |
---|---|
|
The date-time ( |
1.94. Example
This example indicates whether the input is a leap year.
1.94.1. 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|)
}
1.94.2. 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.
1.95. Parameters
Name | Description |
---|---|
|
The date ( |
1.96. Example
This example indicates whether the input is a leap year.
1.96.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"leapYear" : isLeapYear(|2016-10-01|),
"leapYear": isLeapYear(|2017-10-01|)
}
1.96.2. 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.
1.97. Parameters
Name | Description |
---|---|
|
A local date-time ( |
1.98. Example
This example indicates whether the input is a leap year.
1.98.1. 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|)
}
1.98.2. Output
1
2
3
4
{
"leapYear": true,
"leapYear": false
}
1.98.3. isOdd
isOdd(Number): Boolean
Returns true
if the given number is odd, false
if not.
1.99. Parameters
Name | Description |
---|---|
|
A number ( |
1.100. Example
This example indicates whether the input numbers are odd.
1.100.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"isOdd" :
[ isOdd(0), isOdd(1), isOdd(2) ]
}
1.100.2. Output
1
2
3
4
5
6
7
{
"isOdd": [
false,
true,
false
]
}
1.100.3. 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
.
1.101. Parameters
Name | Description |
---|---|
|
The list (an |
|
A |
1.102. Example
This example joins the elements with a hyphen (-
).
1.102.1. Source
1
2
3
4
%dw 2.0
output application/json
---
"hyphenate" : ["a","b","c"] joinBy "-"
1.102.2. Output
1
2
3
{
"hyphenate": "a-b-c"
}
1.102.3. 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.
1.103. Parameters
Name | Description |
---|---|
|
A string that typically describes the log. |
|
The value to log. |
1.104. 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.
1.104.1. Source
1
2
3
4
5
%dw 2.0
output application/xml
var myvar = { "age" : 33 }
---
{ "age": log("My Age", myvar.age) }
1.104.2. Output
1
2
<?xml version="1.0" encoding="UTF-8"?>
<age>33</age>
1.104.3. lower
lower(String): String
Returns the provided string in lowercase characters.
1.105. Parameters
Name | Description |
---|---|
|
A string ( |
1.106. Example
This example converts uppercase characters to lower-case.
1.106.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : lower("MULESOFT")
}
1.106.2. Output
1
2
3
{
"name": "mulesoft"
}
lower(Null): Null
Helper function that allows lower to work with null values.
1.106.3. 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.
1.107. Parameters
Name | Description |
---|---|
|
The list ( |
|
Function used to transform each item in the list. It receives an |
1.108. Example
This example concatenates the index (plus 1) to each value of the array.
1.108.1. Source
1
2
3
4
%dw 2.0
output application/json
---
['joe', 'pete', 'matt'] map ((item, index) -> (index + 1) ++ '_' ++ item)
1.108.2. Output
1
2
3
4
5
[
"1_joe",
"2_pete",
"3_matt"
]
1.109. Example
This example creates an object with the array values, using the index (plus 1) as keys.
1.109.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
(["joe", "pete", "matt"] map (item, index) -> {(index + 1): item})
}
1.109.2. Output
1
2
3
4
5
{
"1": "joe",
"2": "pete",
"3": "matt"
}
1.110. 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.
1.110.1. Source
1
2
3
4
%dw 2.0
output application/json
---
["joe", "pete", "matt"] map (($$ + 1) ++ ":" ++ upper($))
1.111. Example
This example shows how to turn an array of key-value pairs into an object.
1.111.1. 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.
1.111.2. 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.
1.112. Parameters
Name | Description |
---|---|
|
The object to transform. |
|
Function that receives the |
1.113. Example
This example increases each price by 5 and formats the numbers to always include 2 decimals.
1.113.1. 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"}
}
}
1.114. 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>
1.114.1. 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.
1.114.2. 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.
1.115. Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
1.116. 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.
1.116.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "hello" : "anniepoint@mulesoft.com" match(/([a-z]*)@([a-z]*).com/) }
1.116.2. Output
1
2
3
4
5
6
7
{
"hello": [
"anniepoint@mulesoft.com",
"anniepoint",
"mulesoft"
]
}
1.116.3. 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.
1.117. Parameters
Name | Description |
---|---|
|
The input string. |
|
A regular expression for matching characters in the string. |
1.118. 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+/)
).
1.118.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"match" : "admin123" matches /a.*\d+/,
"nonmatch" : "admin123" matches /^b.+/
}
1.118.2. Output
1
2
3
4
{
"match": true,
"nonmatch": false
}
1.118.3. 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.
1.119. Example
This example returns the maximum value of each input array.
1.119.1. 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])
}
1.119.2. Output
1
2
3
4
5
{
"a": 1000,
"b": 3,
"d": 3.5
}
1.119.3. 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.
1.120. Parameters
Name | Description |
---|---|
|
Element in the given array (of type |
1.121. 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
1.121.1. Source
1
2
3
4
%dw 2.0
output application/json
---
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy ((item) -> item.a)
1.121.2. Output
1
{ "a" : 3 }
1.121.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.
1.122. Example
This example returns the lowest numeric value of each input array.
1.122.1. 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])
}
1.122.2. Output
1
2
3
4
5
{
"a": 1,
"b": 1,
"d": 1.5
}
1.122.3. 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.
1.123. Parameters
Name | Description |
---|---|
|
Element in the list (of type |
1.124. 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 $
1.124.1. Source
1
2
3
4
%dw 2.0
output application/json
---
[ { "a" : 1 }, { "a" : 2 }, { "a" : 3 } ] minBy (item) -> item.a
1.124.2. Output
1
{ "a" : 1 }
1.124.3. mod
mod(Number, Number): Number
Returns the modulo (the remainder after performing dividing the dividend
by the divisor
).
1.125. Parameters
Name | Description |
---|---|
|
The number that serves as the dividend for the operation. |
|
The number that serves as the divisor for the operation. |
1.126. 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
).
1.126.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
}
1.126.2. Output
1
2
3
4
5
{
"a": 1,
"b": 0,
"c": 0.2
}
1.126.3. native
native(String): Nothing
Internal method used to indicate that a function implementation is not written in DataWeave but in Scala.
1.126.4. now
now(): DateTime
Returns a DateTime
object with the current date and time.
1.127. 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.
1.127.1. 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
}
1.127.2. 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
}
1.127.3. 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 $
.
1.128. Parameters
Name | Description |
---|---|
|
The object to reorder. |
|
The result of the function is used as the criteria to reorder the object. |
1.129. Example
This example alphabetically orders the values of each object in the array.
Note that orderBy($.letter)
produces the same result as orderBy($[0])
.
1.129.1. Source
1
2
3
4
%dw 2.0
output application/json
---
orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy($.letter)
1.129.2. 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"
}
]
}
1.130. 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:
1.130.1. Source
1
2
3
4
%dw 2.0
output application/json
---
orderDescending: ([3,8,1] orderBy -$)
1.130.2. Output
1
{ "orderDescending": [8,3,1] }
orderBy(Array<T>, (item: T, index: Number) → R): Array<T>
Sorts an array using the specified criteria.
1.131. 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) |
1.132. Example
This example sorts an array of numbers based on the numeric values.
1.132.1. Source
1
2
3
4
%dw 2.0
output application/json
---
[3,2,3] orderBy $
1.132.2. Output
1
2
3
4
5
[
2,
3,
3
]
1.133. Example
This example sorts an array of people based on their age.
1.133.1. 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
1.133.2. 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.
1.133.3. 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.
1.134. Parameters
Name | Description |
---|---|
|
The object to map. |
|
The |
1.135. Example
This example uses pluck
to iterate over each element (object) within
<prices/>
and returns arrays of their keys, values, and indices.
1.135.1. 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($$$)
}
1.135.2. 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
]
}
}
1.136. 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])
.
1.136.1. 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)
1.136.2. 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.
1.136.3. pow
pow(Number, Number): Number
Raises the value of a given base
number to the specified power
.
1.137. Parameters
Name | Description |
---|---|
|
A number ( |
|
A number ( |
1.138. 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
).
1.138.1. 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
}
1.138.2. Output
1
2
3
4
5
{
"a": 8,
"b": 9,
"c": 343
}
1.138.3. random
random(): Number
Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0.
1.139. Example
This example generates a pseudo-random number and multiplies it by 1000.
1.139.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ price: random() * 1000 }
1.139.2. Output
1
{ "price": 65.02770292248383 }
1.139.3. randomInt
randomInt(Number): Number
Returns a pseudo-random whole number from 0 to the specified number (exclusive).
1.140. Parameters
Name | Description |
---|---|
|
A number that sets the upper bound of the random number. |
1.141. Example
This example returns an integer from 0 to 1000 (exclusive).
1.141.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ price: randomInt(1000) }
1.141.2. Output
1
{ "price": 442.0 }
1.141.3. 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.
1.142. 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. |
1.143. 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).
1.143.1. Source
%dw 2.0 var myVar = "Some, Body" output application/json --- read(myVar,"application/csv",{header:false})[0]
1.143.2. Output
1
2
3
4
{
"column_0": "Some",
"column_1": " Body"
}
1.144. Example
This example reads the input XML and shows the syntax for a reader property.
1.144.1. 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"
}
1.144.2. 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>
1.144.3. 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
.
1.145. 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. |
1.146. Example
This example reads JSON object from a URL. (For readability, the output values
shown below are shortened (…
).)
1.146.1. Source
1
2
3
4
%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
1.146.2. Output
1
2
3
4
5
6
{
"userId": 1,
"id": 1,
"title": "sunt aut facere ...",
"body": "quia et suscipit\nsuscipit ..."
}
1.146.3. 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.
1.147. Parameters
Name | Description |
---|---|
|
Item in the given list. It provides the value to reduce. Can also be referenced as |
|
An accumulator (also referenced as |
1.148. Example
This example returns the sum of the values in the input arrays.
1.148.1. 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)
}
1.148.2. Output
1
2
3
4
{
"sum": 15,
"sum": 15
}
1.149. Example
This example uses the accumulator to concatenate elements in the input arrays and return the results in a string.
1.149.1. 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)
}
1.149.2. Output
1
2
3
4
{
"concat": "abcd",
"concat": "abcd"
}
1.150. Example
This example sets the first elements of the arrays to "z"
and 3
.
1.150.1. 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)
}
1.150.2. Output
1
2
3
4
{
"concat": "zabcd"
"sum": 18
}
1.151. Example
This example shows a variety of uses of reduce
, including its application to
arrays of boolean values and objects.
1.151.1. 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)
}
1.151.2. 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
1.151.3. 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.
1.152. Parameters
Name | Description |
---|---|
|
A string ( |
|
A regular expression for matching characters in the |
1.153. 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")
).
1.153.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "replace" : "admin123" replace /(\d+)/ with "ID" }
1.153.2. Output
1
2
3
{
"replace": "adminID"
}
replace(String, String): ((Array<String>, Number) → String) → String
Replaces part of a string with another string.
1.154. Parameters
Name | Description |
---|---|
|
A string ( |
|
A string ( |
1.155. Example
This example replaces the numbers at the end of a string with different characters.
1.155.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "replace": "admin123" replace "123" with "ID" }
1.155.2. Output
1
2
3
{
"replace": "adminID"
}
1.155.3. round
round(Number): Number
Rounds an input number up or down to the nearest whole number.
1.156. Parameters
Name | Description |
---|---|
|
The number to round. |
1.157. Example
This example rounds decimal numbers to the nearest whole numbers.
1.157.1. 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)
}
1.157.2. Output
1
2
3
4
5
{
"a": 1,
"b": 5,
"c": 4
}
1.157.3. 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).
1.158. Parameters
Name | Description |
---|---|
|
The text to scan ( |
|
A regular expression that describes the pattern to look for in the text. |
1.159. 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.
1.159.1. Source
1
2
3
4
%dw 2.0
output application/json
---
"hello" : "anypt@mulesoft.com,max@mulesoft.com" scan /([a-z]*)@([a-z]*).com/
1.159.2. 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"
]
]
}
1.159.3. sizeOf
sizeOf(Array<Any>): Number
Returns the number of elements in an array.
Returns 0
if the array is empty.
1.160. Example
This example returns a count of elements in the input array.
1.160.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"arraySize": sizeOf([1,2,3])
}
1.160.2. 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.
1.161. Example
This example counts the key-value pairs in an input object.
1.161.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
objectSize: sizeOf({a:1,b:2})
}
1.161.2. Output
1
2
3
{
"objectSize": 2
}
sizeOf(Binary): Number
Returns the byte length of a binary.
1.162. 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
.
1.162.1. 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) }
1.162.2. 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.
1.163. Example
This example returns the number of characters in the input strings.
1.163.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"sizeOfString1" : sizeOf("MuleSoft"),
"sizeOfSting2" : sizeOf("my string")
}
1.163.2. Output
1
2
3
4
{
"sizeOfString1": 8,
"sizeOfSting2": 9
}
1.163.3. 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
.
1.164. 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. |
1.165. 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.
1.165.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy(/^*.b./) }
1.165.2. 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
.
1.166. Parameters
Name | Description |
---|---|
|
The string to split. |
|
A string used to separate the input string. It must match some part of the string. |
1.167. Example
This example uses the hyphen (-
) as the separator. Note that the selector is
not retained in the output.
1.167.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "split" : "a-b-c" splitBy("-") }
1.167.2. Output
1
2
3
{
"split": ["a", "b", "c"]
}
1.167.3. sqrt
sqrt(Number): Number
Returns the square root of an input number.
1.168. Parameters
Name | Description |
---|---|
|
The number to apply the operation to. |
1.169. Example
This example returns the square root of an input numbers.
1.169.1. Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
"a" : sqrt(4),
"b" : sqrt(25),
"c" : sqrt(100)
}
1.169.2. Output
1
2
3
4
5
{
"a": 2.0,
"b": 5.0,
"c": 10.0
}
1.169.3. startsWith
startsWith(String, String): Boolean
Returns true
or false
depending on whether the input string starts with a
matching prefix.
1.170. Parameters
Name | Description |
---|---|
|
The input string. |
|
A string that identifies the prefix. |
1.171. 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")
).
1.171.1. Source
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"yes" : "Mariano" startsWith "Mar",
"no" : "Mariano" startsWith "Em"
}
1.171.2. Output
1
2
3
4
{
"yes": true,
"no": false
}
1.171.3. 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.
1.172. Example
This example returns the sum of the values in the input array.
1.172.1. Source
1
2
3
4
%dw 2.0
output application/json
---
sum([1, 2, 3])
1.172.2. Output
1
6
1.172.3. to
to(Number, Number): Range
Returns a range with the specified boundaries.
The upper boundary is inclusive.
1.173. Parameters
Name | Description |
---|---|
|
A number ( |
|
A number ( |
1.174. Example
This example lists a range of numbers from 1 to 10.
1.174.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"myRange": 1 to 10
}
1.174.2. Output
1
2
3
{
"myRange": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
1.174.3. trim
trim(String): String
Removes any blank spaces from the beginning and ending of a string.
1.175. Parameters
Name | Description |
---|---|
|
The string from which to remove any blank spaces. |
1.176. Example
This example trims a string.
1.176.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"trim": trim(" my long text ")
}
1.176.2. Output
1
2
3
{
"trim": "my long text"
}
trim(Null): Null
Helper function that allows trim to work with null values.
1.176.3. typeOf
typeOf(T): Type<T>
Returns the type of an input value.
1.177. Parameters
Name | Description |
---|---|
|
A string, object, array, number, or other supported type. |
1.178. Example
This example identifies the type of the input string.
1.178.1. Source
1
2
3
4
%dw 2.0
output application/json
---
typeOf("A Text")
1.178.2. Output
1
"String"
1.178.3. 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.
1.179. 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.
1.179.1. 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"]])
}
1.179.2. Output
1
2
3
4
5
6
7
8
9
{
"a":[
[0, 1, 2, 3],
["a", "b", "c", "d"]
],
"b": [
[0,1,2,3]
]
}
1.179.3. upper
upper(String): String
Returns the provided string in upper-case characters.
1.180. Parameters
Name | Description |
---|---|
|
The string to convert to uppercase. |
1.181. Example
This example converts lowercase characters to uppercase.
1.181.1. Source
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"name" : upper("mulesoft")
}
1.181.2. Output
1
2
3
{
"name": "MULESOFT"
}
upper(Null): Null
Helper function that allows trim to work with null values.
1.181.3. uuid
uuid(): String
Returns a v4 UUID using random numbers as the source.
1.182. Example
This example generates a random v4 UUID.
1.182.1. Source
%dw 2.0 output application/json --- uuid()
1.182.2. Output
1
"7cc64d24-f2ad-4d43-8893-fa24a0789a99"
1.182.3. with
with(((V, U) → R) → X, (V, U) → R): X
When used with replace
, with
passes a replacement.
1.183. Example
This example replaces all numbers in a string with "x" characters.
1.183.1. Source
1
2
3
4
%dw 2.0
output application/json
---
{ "ssn" : "987-65-4321" replace /[0-9]/ with("x") }
1.183.2. Output
1
{ "ssn": "xxx-xx-xxxx" }
1.183.3. 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.
1.184. 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. |
1.185. 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.
1.185.1. 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"
}
]
1.185.2. Source
1
2
3
4
5
6
%dw 2.0
output application/xml
---
{
"output" : write(payload, "application/csv", {"header":true, "separator" : "|"})
}
1.185.3. 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>
1.185.4. 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.
1.186. 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. |
1.187. 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.
1.187.1. 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"]
}
1.187.2. 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"]
]
}
1.188. Example
This example zips more than two input lists.
1.188.1. 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
1.188.2. 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"]
]
]
1.189. Types
1.189.1. 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
1.189.2. 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
1.189.3. Binary
A blob.
1
Binary
1.189.4. Boolean
A Boolean
type of true
or false
.
1
Boolean
1.189.5. 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
.
1.189.6. 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
}
}
1.189.7. 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}
1.189.8. 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
1.189.9. Date
A date represented by a year, month, and day. For example: |2018-09-17|
1
Date
1.189.10. DateTime
A Date
and Time
within a TimeZone
. For example: |2018-09-17T22:13:00Z|
1
DateTime
1.189.11. Dictionary
Generic dictionary interface.
1
{ _?: T }
1.189.12. 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}
1.189.13. 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}
1.189.14. Key
A key of an Object
.
1
Key
1.189.15. LocalDateTime
A DateTime
in the current TimeZone
. For example: |2018-09-17T22:13:00|
1
LocalDateTime
1.189.16. LocalTime
A Time
in the current TimeZone
. For example: |22:10:18|
1
LocalTime
1.189.17. 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}
1.189.18. Namespace
A Namespace
type represented by a URI
and a prefix.
1
Namespace
1.189.19. Nothing
Bottom type. This type can be assigned to all the types.
1
Nothing
1.189.20. Null
A Null type.
1
Null
1.189.21. Number
A number type: Any number, decimal, or integer is represented by the Number` type.
1
Number
1.189.22. Object
Type that represents any object, which is a collection of Key
and value pairs.
1
Object
1.189.23. Period
A period.
1
Period
1.189.24. Range
A Range
type represents a sequence of numbers.
1
Range
1.189.25. Regex
A regex type.
1
Regex
1.189.26. SimpleType
A union type that represents all the simple types.
1
String | Boolean | Number | DateTime | LocalDateTime | Date | LocalTime | Time | TimeZone | Period
1.189.27. String
These are the native types of DataWeave.
They are the only types that allow the ???
definition.
1
String
1.189.28. Time
A time in a specific TimeZone
. For example: |22:10:18Z|
1
Time
1.189.29. TimeZone
A time zone.
1
TimeZone
1.189.30. Type
Represents a type in the DataWeave type system.
1
Type
1.189.31. Uri
A Uri.
1
Uri
2. dw::Crypto
This module provide functions that perform encryptions through common algorithms, such as MD5, SHA1, and so on.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::Crypto
to the header of your
DataWeave script.
2.1. Functions
2.1.1. HMACBinary
HMACBinary(Binary, Binary): Binary
Computes an HMAC hash (with a secret cryptographic key) on input content.
See also, HMACWith
.
2.2. Parameters
Name | Description |
---|---|
|
The input content, a |
|
The secret cryptographic key (a |
2.3. Example
This example uses HMAC with a secret value to encrypt the input content.
2.3.1. Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "HMACBinary" : Crypto::HMACBinary("key_re_loca" as Binary, "xxxxx" as Binary) }
2.3.2. Output
1
{ "HMACBinary": ".-\ufffd\ufffd\u0012\ufffdÛŠ\ufffd\ufffd\u0000\ufffd\u0012\u0018R\ufffd\ufffd=\ufffd*" }
2.3.3. HMACWith
HMACWith(Binary, Binary): String
Computes an HMAC hash (with a secret cryptographic key) on input content, then * transforms the result into a lowercase, hexadecimal string.
See also, HMACBinary
.
2.4. Parameters
Name | Description |
---|---|
|
The input content, a |
|
The secret cryptographic key (a |
2.5. Example
This example uses HMAC with a secret value to encrypt the input content.
2.5.1. Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "HMACWith" : Crypto::HMACWith("key_re_loca" as Binary, "xxxxx" as Binary) }
2.5.2. Output
1
{ "HMACWith": "2e2da2e51286db8afa9900f51218529cda3dd32a" }
2.5.3. MD5
MD5(Binary): String
Computes the MD5 hash and transforms the binary result into a hexadecimal lower case string.
2.6. Parameters
Name | Description |
---|---|
|
A binary input value to encrypt. |
2.7. Example
This example uses the MD5 algorithm to encrypt a binary value.
2.7.1. Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "md5" : Crypto::MD5("asd" as Binary) }
2.7.2. Output
1
2
3
4
%dw 2.0
import dw::Crypto
---
{ "md5": "7815696ecbf1c96e6894b779456d330e" }
2.7.3. SHA1
SHA1(Binary): String
Computes the SHA1 hash and transforms the result into a hexadecimal, lowercase string.
2.8. Parameters
Name | Description |
---|---|
|
A binary input value to encrypt. |
2.9. Example
This example uses the SHA1 algorithm to encrypt a binary value.
2.9.1. Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "sha1" : Crypto::SHA1("dsasd" as Binary) }
2.9.2. Output
1
{ "sha1": "2fa183839c954e6366c206367c9be5864e4f4a65" }
2.9.3. hashWith
hashWith(Binary, String): Binary
Computes the hash of binary content with a specified algorithm.
You can use these names as algorithm
arguments:
Name | Description |
---|---|
MD2 |
The MD2 message digest algorithm as defined in RFC 1319[http://www.ietf.org/rfc/rfc1319.txt]. |
MD5 |
The MD5 message digest algorithm as defined in RFC 1321[http://www.ietf.org/rfc/rfc1321.txt]. |
SHA-1, SHA-256, SHA-384, SHA-512 |
Hash algorithms defined in the FIPS PUB 180-2 [http://csrc.nist.gov/publications/fips/index.html]. SHA-256 is a 256-bit hash function intended to provide 128 bits of security against collision attacks, while SHA-512 is a 512-bit hash function intended to provide 256 bits of security. A 384-bit hash may be obtained by truncating the SHA-512 output. |
2.10. Parameters
Name | Description |
---|---|
|
A binary input value to encrypt. |
|
The name of the algorithm to use for encrypting the |
2.11. Example
This example uses the MD2 algorithm to encrypt a binary value.
2.11.1. Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "md2" : Crypto::hashWith("hello" as Binary, "SHA-256") }
2.11.2. Output
1
{ "md2": "\ufffd\u0004ls\ufffd\u00031\ufffdh\ufffd}8\u0004\ufffd\u0006U" }
3. dw::Runtime
This module contains functions that allow you to interact with the DataWeave engine.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::Runtime
to the header of your
DataWeave script.
3.1. Functions
3.1.1. fail
fail(String): Nothing
Throws an exception with the specified message.
3.2. Parameters
Name | Description |
---|---|
|
An error message ( |
3.3. Example
This example returns a failure message Data was empty
because the expression
(sizeOf(myVar) ⇐ 0)
is true
. A shortened version of the error message
is shown in the Output section below.
3.3.1. Source
1
2
3
4
5
6
%dw 2.0
import * from dw::Runtime
var result = []
output application/json
---
if(sizeOf(result) <= 0) fail('Data was empty') else result
3.3.2. Output
1
2
3
4
ERROR 2018-07-29 11:47:44,983 ...
*********************************
Message : "Data was empty
...
3.3.3. failIf
failIf(T, (value: T) → Boolean, String): T
Produces an error with the specified message if the expression in
the evaluator returns true
. If not, return the value.
3.4. Parameters
Name | Description |
---|---|
|
The value to return only if the |
|
Expression that returns |
3.5. Example
This example produces a runtime error (instead of a SUCCESS message) because
the expression isEmpty(result)
is true
. It is true
because an empty
object is passed through variable result
.
3.5.1. Source
1
2
3
4
5
6
7
%dw 2.0
import failIf from dw::Runtime
var result1 = {}
var result2 =
output application/json
---
{ "result" : "SUCCESS" failIf (isEmpty(result)) }
3.5.2. Output
1
2
3
ERROR 2018-07-29 11:56:39,988 ...
**********************************
Message : "Failed
3.5.3. locationString
locationString(Any): String
Returns the location string of a given value.
3.6. Parameters
Name | Description |
---|---|
|
A value of any type. |
3.7. Example
This example returns the contents of the line (the location) that defines
variable a
in the header of the DataWeave script.
3.7.1. Source
1
2
3
4
5
6
%dw 2.0
import * from dw::Runtime
output application/json
var a = 123
---
locationString(a)
3.7.2. Output
1
"var a = 123"
3.7.3. prop
prop(String): String | Null
Returns the value of the property with the specified name or null
if the
property is not defined.
3.8. Parameters
Name | Description |
---|---|
|
The property to retrieve. |
3.9. Example
This example gets the user.timezone
property.
3.9.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::Runtime
output application/dw
---
{ "props" : prop("user.timezone") }
3.9.2. Output
1
{ props: "America/Los_Angeles" as String {class: "java.lang.String"} }
3.9.3. props
props(): Dictionary<String>
Returns all the properties configured for Mule runtime.
3.10. Example
This example returns all properties from the java.util.Properties
class.
3.10.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::Runtime
output application/dw
---
{ "props" : props() }
3.10.2. 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
props: {
"java.vendor": "Oracle Corporation" as String {class: "java.lang.String"},
"sun.java.launcher": "SUN_STANDARD" as String {class: "java.lang.String"},
"sun.management.compiler": "HotSpot 64-Bit Tiered Compilers" as String ..., * "os.name": "Mac OS X" as String {class: "java.lang.String"},
"sun.boot.class.path": "/Library/Java/JavaVirtualMachines/ ...,
"org.glassfish.grizzly.nio.transport.TCPNIOTransport...": "1048576" ...,
"java.vm.specification.vendor": "Oracle Corporation" as String ...,
"java.runtime.version": "1.8.0_111-b14" as String {class: "java.lang.String"},
"wrapper.native_library": "wrapper" as String {class: "java.lang.String"},
"wrapper.key": "XlIl4YartmfEU3oKu7o81kNQbwhveXi-" as String ...,
"user.name": "me" as String {class: "java.lang.String"},
"mvel2.disable.jit": "TRUE" as String {class: "java.lang.String"},
"user.language": "en" as String {class: "java.lang.String"} ...,
"sun.boot.library.path": "/Library/Java/JavaVirtualMachines ...
"xpath.provider": "com.mulesoft.licm.DefaultXPathProvider" ...,
"wrapper.backend": "pipe" as String {class: "java.lang.String"},
"java.version": "1.8.0_111" as String {class: "java.lang.String"},
"user.timezone": "America/Los_Angeles" as String {class: "java.lang.String"},
"java.net.preferIPv4Stack": "TRUE" as String {class: "java.lang.String"},
"sun.arch.data.model": "64" as String {class: "java.lang.String"},
"java.endorsed.dirs": "/Library/Java/JavaVirtualMachines/...,
"sun.cpu.isalist": "" as String {class: "java.lang.String"},
"sun.jnu.encoding": "UTF-8" as String {class: "java.lang.String"},
"mule.testingMode": "" as String {class: "java.lang.String"},
"file.encoding.pkg": "sun.io" as String {class: "java.lang.String"},
"file.separator": "/" as String {class: "java.lang.String"},
"java.specification.name": "Java Platform API Specification" ...,
"java.class.version": "52.0" as String {class: "java.lang.String"},
"jetty.git.hash": "82b8fb23f757335bb3329d540ce37a2a2615f0a8" ...,
"user.country": "US" as String {class: "java.lang.String"},
"mule.agent.configuration.folder": "/Applications/AnypointStudio.app/ ...,
"log4j.configurationFactory": "org.apache.logging.log4j.core...",
"java.home": "/Library/Java/JavaVirtualMachines/...,
"java.vm.info": "mixed mode" as String {class: "java.lang.String"},
"wrapper.version": "3.5.34-st" as String {class: "java.lang.String"},
"os.version": "10.13.4" as String {class: "java.lang.String"},
"org.eclipse.jetty.LEVEL": "WARN" as String {class: "java.lang.String"},
"path.separator": ":" as String {class: "java.lang.String"},
"java.vm.version": "25.111-b14" as String {class: "java.lang.String"},
"wrapper.pid": "5212" as String {class: "java.lang.String"},
"java.util.prefs.PreferencesFactory": "com.mulesoft.licm..."},
"wrapper.java.pid": "5213" as String {class: "java.lang.String"},
"mule.home": "/Applications/AnypointStudio.app/...,
"java.awt.printerjob": "sun.lwawt.macosx.CPrinterJob" ...,
"sun.io.unicode.encoding": "UnicodeBig" as String {class: "java.lang.String"},
"awt.toolkit": "sun.lwawt.macosx.LWCToolkit" ...,
"org.glassfish.grizzly.nio.transport...": "1048576" ...,
"user.home": "/Users/me" as String {class: "java.lang.String"},
"java.specification.vendor": "Oracle Corporation" ...,
"java.library.path": "/Applications/AnypointStudio.app/...,
"java.vendor.url": "http://java.oracle.com/" as String ...,
"java.vm.vendor": "Oracle Corporation" as String {class: "java.lang.String"},
gopherProxySet: "false" as String {class: "java.lang.String"},
"wrapper.jvmid": "1" as String {class: "java.lang.String"},
"java.runtime.name": "Java(TM) SE Runtime Environment" ...,
"mule.encoding": "UTF-8" as String {class: "java.lang.String"},
"sun.java.command": "org.mule.runtime.module.reboot....",
"java.class.path": "%MULE_LIB%:/Applications/AnypointStudio.app...",
"log4j2.loggerContextFactory": "org.mule.runtime.module.launcher...,
"java.vm.specification.name": "Java Virtual Machine Specification" ,
"java.vm.specification.version": "1.8" as String {class: "java.lang.String"},
"sun.cpu.endian": "little" as String {class: "java.lang.String"},
"sun.os.patch.level": "unknown" as String {class: "java.lang.String"},
"com.ning.http.client.AsyncHttpClientConfig.useProxyProperties": "true" ...,
"wrapper.cpu.timeout": "10" as String {class: "java.lang.String"},
"java.io.tmpdir": "/var/folders/42/dd73l3rx7qz0n625hr29kty80000gn/T/" ...,
"anypoint.platform.analytics_base_uri": ...,
"java.vendor.url.bug": "http://bugreport.sun.com/bugreport/" ...,
"os.arch": "x86_64" as String {class: "java.lang.String"},
"java.awt.graphicsenv": "sun.awt.CGraphicsEnvironment" ...,
"mule.base": "/Applications/AnypointStudio.app...",
"java.ext.dirs": "/Users/staceyduke/Library/Java/Extensions: ..."},
"user.dir": "/Applications/AnypointStudio.app/..."},
"line.separator": "\n" as String {class: "java.lang.String"},
"java.vm.name": "Java HotSpot(TM) 64-Bit Server VM" ...,
"org.quartz.scheduler.skipUpdateCheck": "true" ...,
"file.encoding": "UTF-8" as String {class: "java.lang.String"},
"mule.forceConsoleLog": "" as String {class: "java.lang.String"},
"java.specification.version": "1.8" as String {class: "java.lang.String"},
"wrapper.arch": "universal" as String {class: "java.lang.String"}
} as Object {class: "java.util.Properties"}
3.10.3. try
try(() → T): TryResult<T>
Evaluates the delegate function and returns an object with the result or an error message.
3.11. Parameters
Name | Description |
---|---|
|
The function to evaluate. |
3.12. Example
This example passes the fail
function as an argument to try
.
3.12.1. Source
1
2
3
4
5
%dw 2.0
import try, fail from dw::Runtime
output application/json
---
try(fail)
3.12.2. Output
1
2
3
4
5
6
7
8
9
10
11
{
"success": false,
"error": {
"kind": "UserException",
"message": "Error",
"location": "Unknown location",
"stack": [
"main (anonymous:0:0)"
]
}
}
3.12.3. wait
wait(T, Number): T
Stops the execution for the specified timeout (in milliseconds).
3.13. Parameters
Name | Description |
---|---|
|
Input of any type. |
|
The number of milliseconds to wait. |
3.14. Example
This example waits 2000 milliseconds (2 seconds) to execute.
3.14.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::Runtime
output application/json
---
{ "user" : 1 } wait 2000
3.14.2. Output
1
{ "user": 1 }
3.15. Types
3.15.1. TryResult
Object with a result or error message. If success
is false
, it contains
the error
. If true
, it provides the result
.
1
{ success: Boolean, result?: T, error?: { kind: String, message: String, stack?: Array<String>, location?: String } }
4. dw::System
This module contains functions that allow you to interact with the underlying system.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::System
to the header of your
DataWeave script.
4.1. Functions
4.1.1. envVar
envVar(String): String | Null
Returns an environment variable with the specified name or null
if the
environment variable is not defined.
4.2. Parameters
Name | Description |
---|---|
|
Provides the name of the environment variable. |
4.3. Example
This example returns a Mac command console (SHELL
) path and returns null
on FAKE_ENV_VAR
(an undefined environment variable). SHELL
is one of the
standard Mac environment variables. Also notice that the import
command
enables you to call the function without prepending the module name to it.
4.3.1. Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::System
output application/json
---
{
"envVars" : [
"real" : envVar("SHELL"),
"fake" : envVar("FAKE_ENV_VAR")
]
}
4.3.2. Output
1
2
3
4
5
6
7
8
"envVars": [
{
"real": "/bin/bash"
},
{
"fake": null
}
]
4.3.3. envVars
envVars(): Dictionary<String>
Returns all of the environment variables defined in the host system.
4.4. Example
This example returns a Mac command console (SHELL
) path. SHELL
is one of
the standard Mac environment variables. To return all the environment
variables, you can use dw::System::envVars()
.
4.4.1. Source
1
2
3
4
5
%dw 2.0
import dw::System
output application/json
---
{ "envVars" : dw::System::envVars().SHELL }
4.4.2. Output
1
{ "envVars": "/bin/bash" }
5. dw::core::Arrays
This module contains helper functions for working with arrays.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Arrays
to the header of your
DataWeave script.
5.1. Functions
5.1.1. countBy
countBy(Array<T>, (T) → Boolean): Number
Counts the elements in a list (array) that match the results of a function.
5.2. Parameters
Name | Description |
---|---|
|
The input list that contains elements to match. |
|
A function to apply to the input list. |
5.3. Example
This counts the values in the list that are equal to the result of the
matchingFunction
((($ mod 2) == 0)
).
5.3.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{ "countBy" : [1, 2, 3, 4] countBy (($ mod 2) == 0) }
5.3.2. Output
1
{ "countBy": 2 }
5.3.3. divideBy
divideBy(Array<T>, Number): Array<Array<T>>
Breaks up a list (array) into sub-lists (sub-arrays) that contain the specified number of elements.
If there are fewer elements in the input array than the specified number, the function will fill the sub-array with those elements. If there are more elements, the function will fill as many sub-arrays needed with the extra elements.
5.4. Parameters
Name | Description |
---|---|
|
Items in the input array. |
|
The number of elements allowed per sub-array. |
5.5. Example
This example breaks up arrays into sub-arrays based on the specified amount
.
5.5.1. Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
"divideBy" : [
{ "divideBy2" : [1, 2, 3, 4, 5] divideBy 2 },
{ "divideBy2" : [1, 2, 3, 4, 5, 6] divideBy 2 },
{ "divideBy3" : [1, 2, 3, 4, 5] divideBy 3 }
]
}
5.5.2. 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
{
"divideBy": [
{
"divideBy2": [
[ 1, 2 ],
[ 3, 4 ],
[ 5 ]
]
},
{
"divideBy2": [
[ 1, 2 ],
[ 3, 4 ],
[ 5, 6 ]
]
},
{
"divideBy3": [
[ 1, 2, 3 ],
[ 4, 5 ]
]
}
]
}
5.5.3. every
every(Array<T>, (T) → Boolean): Boolean
Returns true
if every element in the list (array) matches the condition.
The function stops iterating after the first negative evaluation of a list.
5.6. Parameters
Name | Description |
---|---|
|
The input list. |
|
A condition (or expression) to apply to the input list. |
5.7. Example
This example applies a variety of expressions to the input lists.
5.7.1. Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
import * from dw::core::Arrays
var arr0 = [] as Array<Number>
output application/json
---
{ "results" : [
"ok" : [
[1,1,1] every ($ == 1),
[1] every ($ == 1)
],
"err" : [
[1,2,3] every ((log('should stop at 2 ==', $) mod 2) == 1),
[1,1,0] every ($ == 1),
[0,1,1,0] every (log('should stop at 0 ==', $) == 1),
[1,2,3] every ($ == 1),
arr0 every true,
]
]
}
5.7.2. Output
1
2
3
4
5
6
7
8
9
10
{
"results": [
{
"ok": [ true, true ]
},
{
"err": [ false, false, false, false, false ]
}
]
}
5.7.3. some
some(Array<T>, (T) → Boolean): Boolean
Returns true
if an element in the list (an array) matches the specified condition.
The function stops iterating after the first match to an element in the list.
5.8. Parameters
Name | Description |
---|---|
|
The input list. |
|
A condition (or expression) to apply to the input list. |
5.9. Example
This example applies a variety of expressions to the input lists.
5.9.1. Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{ "results" : [
"ok" : [
[1,2,3] some (($ mod 2) == 0),
[1,2,3] some (($ mod 2) == 1),
[1,2,3,4,5,6,7,8] some (log('should stop at 2 ==', $) == 2),
[1,2,3] some ($ == 1),
[1,1,1] some ($ == 1),
[1] some ($ == 1)
],
"err" : [
[1,2,3] some ($ == 100),
[1] some ($ == 2)
]
]
}
5.9.2. Output
1
2
3
4
5
6
7
8
9
10
{
"results": [
{
"ok": [ true, true, true, true, true, true ]
},
{
"err": [ false, false ]
}
]
}
5.9.3. sumBy
sumBy(Array<T>, (T) → Number): Number
Returns the sum of the values of the elements in a list (an array).
5.10. Parameters
Name | Description |
---|---|
|
The input list. |
|
A DataWeave selector that selects the values of the numbers in the input list. |
5.11. Example
This example calculates the sum of the values of elements some lists. Notice
that both of the sumBy
function calls produce the same result.
5.11.1. Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
"sumBy" : [
[ { a: 1 }, { a: 2 }, { a: 3 } ] sumBy $.a,
sumBy([ { a: 1 }, { a: 2 }, { a: 3 } ], (item) -> item.a)
]
}
5.11.2. Output
1
{ "sumBy" : [ 6, 6 ] }
6. dw::core::Binaries
This module contains helper functions for working with binaries.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Binaries
to the header of your
DataWeave script.
6.1. Functions
6.1.1. fromBase64
fromBase64(String): Binary
Transforms a Base64 string to binary.
6.2. Parameters
Name | Description |
---|---|
|
The Base64 string to transform. |
6.3. Example
This example takes a Base64 string and transforms it to binary.
6.3.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ "BinaryFromBase64" : fromBase64("TXVsZQ==") }
6.3.2. Output
1
{ "BinaryFromBase64": "Mule" }
6.3.3. fromHex
fromHex(String): Binary
Transforms an hexadecimal string into a binary.
6.4. Parameters
Name | Description |
---|---|
|
A hexadecimal string to transform. |
6.5. Example
This example transforms a hexadecimal string to "Mule".
6.5.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Binaries
output application/json
---
{ "hexToBinary": fromHex("4D756C65") }
6.5.2. Output
1
{ "hexToBinary": "Mule" }
6.5.3. toBase64
toBase64(Binary): String
Transforms a binary value a Base64 string.
6.6. Parameters
Name | Description |
---|---|
|
The binary value to transform. |
6.7. Example
This example transforms a binary to Base64.
6.7.1. Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::Binaries
var myBinary = "Mule" as Binary
output application/json
---
{ "BinaryToBase64" : toBase64(myBinary) }
6.7.2. Output
1
{ "BinaryToBase64": "TXVsZQ==" }
6.7.3. toHex
toHex(Binary): String
Transforms a binary value into the hexadecimal string.
6.8. Parameters
Name | Description |
---|---|
|
The |
6.9. Example
This example transforms a binary version of "Mule" (defined in the variable,
myBinary
) to hexadecimal.
6.9.1. Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Binaries
output application/json
var myBinary = "Mule" as Binary
var testType = typeOf(myBinary)
---
{
"binaryToHex" : toHex(myBinary)
}
6.9.2. Output
1
{ "binaryToHex": "4D756C65" }
7. dw::core::Objects
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.
7.1. Functions
7.1.1. 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.
7.2. Parameters
Name | Description |
---|---|
|
Key-value pairs in the source object. |
|
The number of key-value pairs allowed in an object. |
7.3. Example
This example breaks up objects into sub-objects based on the specified amount
.
7.3.1. 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 }
7.3.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
}
]
}
7.3.3. entrySet
entrySet(T)
Returns a list of key-value pairs that describe the key, value, and any attributes in the input object.
7.4. Parameters
Name | Description |
---|---|
|
The |
7.5. Example
This example returns the key, value, and attributes in the object specified
in the variable myVar
.
7.5.1. 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) }
7.5.2. 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"
}
}
]
}
7.5.3. keySet
keySet(T): ?
Returns the list of key names from an object.
7.6. Parameters
Name | Description |
---|---|
|
The object to evaluate. |
7.7. Example
This example returns the keys from the input object.
7.7.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Objects
output application/json
---
{ "keySet" : keySet({ "a" : true, "b" : 1}) }
7.7.2. Output
1
{ "keySet" : ["a","b"] }
7.8. 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.
7.8.1. 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 $.@])
}
7.8.2. 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
]
}
7.8.3. 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.
7.9. Parameters
Name | Description |
---|---|
|
The object to append to the |
|
The object to which the |
7.10. 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.
7.10.1. 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"} }
7.10.2. 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.
7.10.3. nameSet
nameSet(Object): Array<String>
Returns a list of keys from an object.
7.11. Parameters
Name | Description |
---|---|
|
The object to evaluate. |
7.12. Example
This example returns the keys from the input object.
7.12.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Objects
output application/json
---
{ "nameSet" : nameSet({ "a" : true, "b" : 1}) }
7.12.2. Output
1
{ "nameSet" : ["a","b"] }
7.12.3. valueSet
valueSet({ (K)?: V }): Array<V>
Returns the list of the values in an object.
7.13. Parameters
Name | Description |
---|---|
|
The object to evaluate. |
7.14. Example
This example returns the values from the input object.
7.14.1. Source
1
2
3
4
5
%dw 2.0
import dw::core::Objects
output application/json
---
{ "valueSet" : valueSet({a: true, b: 1}) }
7.14.2. Output
1
{ "valueSet" : [true,1] }
8. dw::core::Strings
This module contains helper functions to work with Strings.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Strings
to the header of your
DataWeave script.
8.1. Functions
8.1.1. camelize
camelize(String): String
Returns a string in camel case based on underscores in the string.
8.2. Parameters
Name | Description |
---|---|
|
The string to convert to camel case. |
8.3. Example
This example converts a string that contains underscores to camel case.
8.3.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "camelize" : camelize("customer_first_name") }
8.3.2. Output
1
{ "camelize" : "customerFirstName" }
camelize(Null): Null
Helper function that allows camelize
to work with null values.
8.3.3. capitalize
capitalize(String): String
Capitalizes the first letter of each word in a string.
It also removes underscores between words and puts a space before each capitalized word.
8.4. Parameters
Name | Description |
---|---|
|
The string to capitalize. |
8.5. Example
This example capitalizes a set of strings.
8.5.1. Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : capitalize("customer"),
"b" : capitalize("customer_first_name"),
"c" : capitalize("customer NAME"),
"d" : capitalize("customerName")
}
8.5.2. Output
1
2
3
4
5
6
{
"a": "Customer",
"b": "Customer First Name",
"c": "Customer Name",
"d": "Customer Name"
}
capitalize(Null): Null
Helper function that allows capitalize to work with null values.
8.5.3. charCode
charCode(String): Number
Returns the unicode for the first character in an input string.
For an empty string, the function fails and returns Unexpected empty string
.
8.6. Parameters
Name | Description |
---|---|
|
The input string. |
8.7. Example
This example returns unicode for the "M" in "Mule".
8.7.1. Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"charCode" : charCode("Mule")
}
8.7.2. Output
1
{ "charCode" : 77 }
8.7.3. charCodeAt
charCodeAt(String, Number): Number
Returns the Unicode for a character at the specified index.
This function fails if the index is invalid.
8.8. Parameters
Name | Description |
---|---|
|
The input string. |
|
The index (a |
8.9. Example
This example returns Unicode for the "u" at index 1
in "MuleSoft".
8.9.1. Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"charCodeAt" : charCodeAt("MuleSoft", 1)
}
8.9.2. Output
1
{ "charCodeAt": 117 }
8.9.3. dasherize
dasherize(String): String
Replaces spaces, underscores, and camel-casing in a string with dashes (hyphens).
If no spaces, underscores, and camel-casing are present, the output will match the input.
8.10. Parameters
Name | Description |
---|---|
|
The input string. |
8.11. Example
This example replaces the spaces, underscores, and camel-casing in the input. Notice that the input "customer" is not modified in the output.
8.11.1. Source
1
2
3
4
5
6
{
"a" : dasherize("customer"),
"b" : dasherize("customer_first_name"),
"c" : dasherize("customer NAME"),
"d" : dasherize("customerName")
}
8.11.2. Output
1
2
3
4
5
6
{
"a": "customer",
"b": "customer-first-name",
"c": "customer-name",
"d": "customer-name"
}
dasherize(Null): Null
Helper function that allows dasherize to work with null values.
8.11.3. fromCharCode
fromCharCode(Number): String
Returns a character that matches the input unicode.
8.12. Parameters
Name | Description |
---|---|
|
The input unicode (a |
8.13. Example
This example inputs the unicode number 117
to return the character "u".
8.13.1. Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"fromCharCode" : fromCharCode(117)
}
8.13.2. Output
1
{ "fromCharCode": "u" }
8.13.3. ordinalize
ordinalize(Number): String
Returns a number as an ordinal, such as 1st
or 2nd
.
8.14. Parameters
Name | Description |
---|---|
|
An input number to return as an ordinal. |
8.15. Example
This example returns a variety of input numbers as ordinals.
8.15.1. Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : ordinalize(1),
"b": ordinalize(2),
"c": ordinalize(5),
"d": ordinalize(103)
}
8.15.2. Output
1
2
3
4
5
6
{
"a": "1st",
"b": "2nd",
"c": "5th",
"d": "103rd"
}
ordinalize(Null): Null
Helper function that allows ordinalize
to work with null values.
8.15.3. pluralize
pluralize(String): String
Pluralizes a singular string.
If the input is already plural (for example, "boxes"), the output will match the input.
8.16. Parameters
Name | Description |
---|---|
|
The string to pluralize. |
8.17. Example
This example pluralizes the input string "box" to return "boxes".
8.17.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "pluralize" : pluralize("box") }
8.17.2. Output
1
{ "pluralize" : "boxes" }
pluralize(Null): Null
Helper function that allows pluralize to work with null values.
8.17.3. singularize
singularize(String): String
Converts a plural string to its singular form.
If the input is already singular (for example, "box"), the output will match the input.
8.18. Parameters
Name | Description |
---|---|
|
The string to convert to singular form. |
8.19. Example
This example converts the input string "boxes" to return "box".
8.19.1. Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "singularize" : singularize("boxes") }
8.19.2. Output
1
{ "singularize" : "box" }
singularize(Null): Null
Helper function that allows singularize to work with null values.
8.19.3. underscore
underscore(String): String
Replaces hyphens, spaces, and camel-casing in a string with underscores.
If no hyphens, spaces, and camel-casing are present, the output will match the input.
8.20. Parameters
Name | Description |
---|---|
|
The input string. |
8.21. Example
This example replaces the hyphens and spaces in the input. Notice that the input "customer" is not modified in the output.
8.21.1. Source
1
2
3
4
5
6
{
"a" : underscore("customer"),
"b" : underscore("customer-first-name"),
"c" : underscore("customer NAME"),
"d" : underscore("customerName")
}
8.21.2. Output
1
2
3
4
5
6
{
"a": "customer",
"b": "customer_first_name",
"c": "customer_name",
"d": "customer_name"
}
underscore(Null): Null
Helper function that allows underscore to work with null values.
9. dw::core::URL
This module contains helper functions to work with Strings.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::URL
to the header of your
DataWeave script.
9.1. Functions
9.1.1. compose
compose(Array<String>, Array<String>): String
Uses a custom interpolator to replace URL components with a
encodeURIComponent
result.
9.2. Parameters
Name | Description |
---|---|
|
A string array that provides the part of the URL to encode. |
9.3. Example
This example passes in text to encode using $(myText)
, where myText
is a variable defined in the header. Notice that the spaces in the input are
encoded in the output URL as %20
. Also notice that the entire input to the
compose
function is surrounded by backticks.
9.3.1. Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::URL
var myText = " text to encode "
output application/json
---
{ "composition" : compose `http://asd/$(myText)/text` }
9.3.2. Output
1
{ "composition": "http://asd/%20text%20to%20encode%20/text" }
9.3.3. decodeURI
decodeURI(String): String
Decodes the escape sequences (such as %20
) in a URI.
The function replaces each escape sequence in the encoded URI with the
character that it represents, but does not decode escape sequences that
could not have been introduced by encodeURI
. The character #
is not
decoded from escape sequences.
9.4. Parameters
Name | Description |
---|---|
|
The URI to decode. |
9.5. Example
This example decodes a URI that contains the URL percent encoding %20
,
which is used for spaces.
9.5.1. Source
1
2
3
{
"decodeURI" : decodeURI('http://asd/%20text%20to%20decode%20/text')
}
9.5.2. 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
27
28
29
{
"decodeURI": "http://asd/ text to decode /text"
}
=== decodeURIComponent
==== decodeURIComponent(String): String
Decodes a Uniform Resource Identifier (URI) component previously created
by `encodeURIComponent` or a similar routine.
For an example, see `encodeURIComponent`.
== Parameters
[%header, cols="1,3"]
|===
| Name | Description
| `text` | URI component string.
|===
== Example
This example decodes a variety of URI components.
=== Source
[source,DataWeave, linenums]
%dw 2.0 import * from dw::core::URL output application/json --- { "decodeURIComponent": { "decodeURIComponent" : decodeURIComponent("%20PATH/%20TO%20/DECODE%20"), "decodeURIComponent" : decodeURIComponent("%3B%2C%2F%3F%3A%40%26%3D"), "decodeURIComponent" : decodeURIComponent("%2D%5F%2E%21%7E%2A%27%28%29%24"), } }
=== Output [source,JSON,linenums]
{ decodeURIComponent: { decodeURIComponent: " PATH/ TO /DECODE ", decodeURIComponent: ";,/?:@&=", decodeURIComponent: "-_.!~*'()\$" } }
=== encodeURI ==== encodeURI(String): String Encodes a URI with UTF-8 escape sequences. Applies up to four escape sequences for characters composed of two "surrogate" characters. The function assumes that the URI is a complete URI, so it does not encode reserved characters that have special meaning. The function _does not encode these characters_ with UTF-8 escape sequences: [%header, cols="2,2"] |=== | Type (not escaped) | Examples | Reserved characters | ; , / ? : @ & = $ | Unescaped characters | alphabetic, decimal digits, - _ . ! ~ * ' ( ) | Number sign | # |=== == Parameters [%header, cols="1,3"] |=== | Name | Description | `text` | The URI to encode. |=== == Example This example shows encodes spaces in one URL and lists some characters that do not get encoded in the `not_encoded` string. === Source [source,DataWeave, linenums]
{ "encodeURI" : encodeURI("http://asd/ text to decode /text"), "not_encoded": encodeURI("http://:;,/?:@&=\$-.!~*'()") }
=== Output [source,JSON,linenums]
{ "encodeURI": "http://asd/%20text%20to%20decode%20/%25/\"\'/text", "not_encoded": "http://:;,/?:@&=$-.!~*'()" }
=== encodeURIComponent ==== encodeURIComponent(String): String Escapes certain characters in a URI component using UTF-8 encoding. There can be only four escape sequences for characters composed of two "surrogate" * characters. `encodeURIComponent` escapes all characters _except the following_: alphabetic, decimal digits, `- _ . ! ~ * ' ( )`. Note that `encodeURIComponent` differs from `encodeURI` in that it encodes reserved characters and the Number sign `#` of `encodeURI`: [%header, cols="2,2"] |=== | Type | Includes | Reserved characters | | Unescaped characters | alphabetic, decimal digits, - _ . ! ~ * ' ( ) | Number sign | |=== == Parameters [%header, cols="1,3"] |=== | Name | Description | `text` | URI component string. |=== == Example This example encodes a variety of URI components. === Source [source,DataWeave, linenums]
%dw 2.0 import * from dw::core::URL output application/json --- { "comparing_encode_functions_output" : { "encodeURIComponent" : encodeURI(" PATH/ TO /ENCODE "), "encodeURI" : encodeURI(" PATH/ TO /ENCODE "), "encodeURIComponent_to_hex" : encodeURIComponent(";,/?:@&="), "encodeURI_not_to_hex" : encodeURI(";,/?:@&="), "encodeURIComponent_not_encoded" : encodeURIComponent("-.!~'()"), "encodeURI_not_encoded" : encodeURI("-.!~'()") } }
=== Output [source,JSON,linenums]
{ "comparing_encode_functions_output": { "encodeURIComponent": "%20PATH/%20TO%20/ENCODE%20", "encodeURI": "%20PATH/%20TO%20/ENCODE%20", "encodeURIComponent_to_hex": "%3B%2C%2F%3F%3A%40%26%3D", "encodeURI_not_to_hex": ";,/?:@&=", "encodeURIComponent_not_encoded": "-.!~'()", "encodeURI_not_encoded": "-.!~'()" } }
=== parseURI ==== parseURI(String): URI Parses a URL and returns a `URI` object. The `isValid: Boolean` property in the output `URI` object indicates whether the parsing process succeeded. Every field in this object is optional, and a field will appear in the output only if it was present in the URL input. == Parameters [%header, cols="1,3"] |=== | Name | Description | `uri` | The URI input. |=== == Example This example parses a URL. === Source [source,DataWeave, linenums]
%dw 2.0 import * from dw::core::URL output application/json --- { 'composition': parseURI('https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer') }
=== Output [source,JSON,linenums]
{ "composition": { "isValid": true, "raw": "https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer", "host": "en.wikipedia.org", "authority": "en.wikipedia.org", "fragment": "footer", "path": "/wiki/Uniform_Resource_Identifier", "scheme": "https", "isAbsolute": true, "isOpaque": false } }
== Types === URI .Definition [source,DataWeave,linenums]
{ isValid: Boolean, host?: String, authority?: String, fragment?: String, path?: String, port?: Number, query?: String, scheme?: String, user?: String, isAbsolute?: Boolean, isOpaque?: Boolean }
= dw::util::Diff This utility module calculates the difference between two values and returns the list of differences. The module is included with Mule runtime. To use it, you must import it to your DataWeave code, for example, by adding the line `import dw::util::Diff` or `import * from dw::util::Diff` to your header. == Functions === diff ==== diff(Any, Any, { unordered?: Boolean }, String): Diff Returns the structural differences between two values. Differences between objects can be ordered (the default) or unordered. Ordered means that two objects do not differ if their key-value pairs are in the same order. Differences are expressed as `Difference` type. == Parameters [%header, cols="1,3"] |=== | Name | Description | `actual` | The actual value. Can be any data type. | `expected` | The expected value to compare to the actual. Can be any data type. | `diffConfig` | Setting for changing the default to unordered using `{ "unordered" : true} (explained in the introduction). |=== == Example This example shows a variety of uses of `diff`. === Source [source,Dataweave, linenums]
import diff from dw::util::Diff ns ns0 http://locahost.com ns ns1 http://acme.com output application/dw --- { "a": diff({a: 1}, {b:1}), "b": diff({ns0#a: 1}, {ns1#a:1}), "c": diff([1,2,3], []), "d": diff([], [1,2,3]), "e": diff([1,2,3], [1,2,3, 4]), "f": diff([{a: 1}], [{a: 2}]), "g": diff({a @(c: 2): 1}, {a @(c: 3): 1}), "h": diff(true, false), "i": diff(1, 2), "j": diff("test", "other test"), "k": diff({a: 1}, {a:1}), "l": diff({ns0#a: 1}, {ns0#a:1}), "m": diff([1,2,3], [1,2,3]), "n": diff([], []), "o": diff([{a: 1}], [{a: 1}]), "p": diff({a @(c: 2): 1}, {a @(c:2): 1}), "q": diff(true, true), "r": diff(1, 1), "s": diff("other test", "other test"), "t": diff({a:1 ,b: 2},{b: 2, a:1}, {unordered: true}), "u": [{format: "ssn",data: "ABC"}] diff [{ format: "ssn",data: "ABC"}] }
=== Output [source,XML,linenums]
ns ns0 http://locahost.com ns ns1 http://acme.com --- { a: { matches: false, diffs: [ { expected: "Entry (root).a with type Number", actual: "was not present in object.", path: "(root).a" } ] }, b: { matches: false, diffs: [ { expected: "Entry (root).ns0#a with type Number", actual: "was not present in object.", path: "(root).ns0#a" } ] }, c: { matches: false, diffs: [ { expected: "Array size is 0", actual: "was 3", path: "(root)" } ] }, d: { matches: false, diffs: [ { expected: "Array size is 3", actual: "was 0", path: "(root)" } ] }, e: { matches: false, diffs: [ { expected: "Array size is 4", actual: "was 3", path: "(root)" } ] }, f: { matches: false, diffs: [ { expected: "1" as String {mimeType: "application/dw"}, actual: "2" as String {mimeType: "application/dw"}, path: "(root)[0].a" } ] }, g: { matches: false, diffs: [ { expected: "3" as String {mimeType: "application/dw"}, actual: "2" as String {mimeType: "application/dw"}, path: "(root).a.@.c" } ] }, h: { matches: false, diffs: [ { expected: "false", actual: "true", path: "(root)" } ] }, i: { matches: false, diffs: [ { expected: "2", actual: "1", path: "(root)" } ] }, j: { matches: false, diffs: [ { expected: "\"other test\"", actual: "\"test\"", path: "(root)" } ] }, k: { matches: true, diffs: [] }, l: { matches: true, diffs: [] }, m: { matches: true, diffs: [] }, n: { matches: true, diffs: [] }, o: { matches: true, diffs: [] }, p: { matches: true, diffs: [] }, q: { matches: true, diffs: [] }, r: { matches: true, diffs: [] }, s: { matches: true, diffs: [] }, t: { matches: true, diffs: [] }, u: { matches: true, diffs: [] } }
== Types === Diff Describes the entire difference between two values. .Definition [source,DataWeave,linenums]
{ matches: Boolean, diffs: Array<Difference> }
=== Difference Describes a single difference between two values at a given structure. .Definition [source,DataWeave,linenums]
{ expected: String, actual: String, path: String }
= dw::util::Timer This is a utility module. The module is included with Mule runtime. To use it, you must import it to your DataWeave code, for example, by adding the line `import * from dw::util::Timer` to the header of your script. == Functions === currentMilliseconds ==== currentMilliseconds(): Number Returns the current time in milliseconds. == Example This example shows the time in milliseconds when the function executed. === Source [source,Dataweave, linenums]
%dw 2.0 import * from dw::util::Timer output application/json --- { "currentMilliseconds" : currentMilliseconds() }
=== Output [source,XML,linenums]
{ "currentMilliseconds": 1532923168900 }
=== duration ==== duration(() -> T): DurationMeasurement<T> Executes the function and returns an object with the taken time in milliseconds with the result of the function. == Parameters [%header, cols="1,3"] |=== | Name | Description | `valueToMeasure` | A function to pass to `duration`. |=== == Example This example passes a `wait` function (defined in the header), which returns the result in a `DurationMeasurement` object. === Source [source,Dataweave, linenums]
%dw 2.0 output application/json fun myFunction() = dw::Runtime::wait("My result",100) --- dw::util::Timer::duration) → myFunction(
=== Output [source,XML,linenums]
{ "time": 101, "result": "My result" }
=== time ==== time(() -> T): TimeMeasurement<T> Executes the specified function and returns an object with the start time and end time with the result of the function. == Parameters [%header, cols="1,3"] |=== | Name | Description | `valueToMeasure` | A function to pass to `time`. |=== == Example This example passes a `wait` function (defined in the header), which returns the result in a `TimeMeasurement` object. [source,Dataweave, linenums]
%dw 2.0 output application/json fun myFunction() = dw::Runtime::wait("My result",100) --- dw::util::Timer::time) → myFunction(
=== Output [source,XML,linenums]
{ "start": "2018-09-26T18:58:00.887Z", "result": "My result", "end": "2018-09-26T18:58:00.988Z" }
=== toMilliseconds ==== toMilliseconds(DateTime): Number Returns the representation of a specified date-time in milliseconds. == Parameters [%header, cols="1,3"] |=== | Name | Description | `date` | A `DateTime` to evaluate. |=== == Example This example shows a date-time in milliseconds. === Source [source,Dataweave, linenums]
%dw 2.0 import * from dw::util::Timer output application/json --- { "toMilliseconds" : toMilliseconds(|2018-07-23T22:03:04.829Z|) }
=== Output [source,XML,linenums]
{ "toMilliseconds": 1532383384829 }
== Types === DurationMeasurement Represents a time taken by a function call. .Definition [source,DataWeave,linenums]
{ time: Number, result: T }
=== TimeMeasurement Represents a start/end time measurement. .Definition [source,DataWeave,linenums]
{ start: DateTime, result: T, end: DateTime }
:leveloffset: 0