This module contains all the core data weave functionality. It is automatically imported into any data weave script.

Functions

++

++(Array<S>, Array<T>): Array<S | T>

Concatenates the elements of two arrays into a new array.

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.

Here is an example of concatenating an Array<Number> with an Array<String>:

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  result: [0, 1, 2] ++ ["a", "b", "c"]
}
Output
1
2
3
{
  "result": [0, 1, 2, "a", "b", "c"]
}

Note that the arrays can contain any supported data type, for example:

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  a: [0, 1, true, "my string"] ++ [2, [3,4,5], {"a": 6}]
}
Output
1
2
3
{
  "a": [0, 1, true, "my string", 2, [3, 4, 5], { "a": 6}]
}

++(String, String): String

Strings are treated as arrays of characters, so the ++ operator concatenates the characters of each String as if they were arrays of single character Strings.

For example, the String "Mule" is treated like the Array<String> ["M", "u", "l", "e"].

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  name: "Mule" ++ "Soft"
}
Output
1
2
3
{
  "name": MuleSoft
}

++(Object, Object): Object

Concatenates two input objects together and returns one flattened object.

The ++ operator extracts all the key-values pairs from each object, then combines them together into one result object.

Transform
1
2
3
4
%dw 2.0
output application/xml
---
concat: {aa: "a", bb: "b"} ++ {cc: "c"}
Output
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<concat>
  <aa>a</aa>
  <bb>b</bb>
  <cc>c</cc>
</concat>

If you leave the output as application/dw, the example above concatenates each key-value pair from the two objects {aa: "a", bb: "b"} ++ {cc: "c"} and returns a single object {aa: "a" , bb: "b", cc: "c"}.

++(Date, LocalTime): LocalDateTime

You can append a Time (or LocalTime) with a Date object to return a more precise Datetime value.

Date and Time instances are written in standard Java notation, surrounded by pipe (|) symbols. The result is a DateTime object in the standard Java format.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: |2003-10-01| ++ |23:57:59|,
  b: |2003-10-01| ++ |23:57:59Z|
}
Output
1
2
3
4
{
    "a": "2003-10-01T23:57:59",
    "b": "2003-10-01T23:57:59Z"
}

Note that the order in which the two objects are appended is irrelevant, so logically, a 'Date' + 'Time' will produce the same result as a 'Time' + 'Date'.

++(LocalTime, Date): LocalDateTime

You can append a date to a time (or localtime) object so as to provide a more precise value.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: |23:57:59| ++ |2003-10-01|,
  b: |23:57:59Z| ++ |2003-10-01|
}
Output
1
2
3
4
{
    "a": "2003-10-01T23:57:59",
    "b": "2003-10-01T23:57:59Z"
}

Note that the order in which the two objects are appended is irrelevant, so logically, a 'Date' + 'Time' will produce the same result as a 'Time' + 'Date'.

++(Date, Time): DateTime

You can append a date to a time (or localtime) object so as to provide a more precise value.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: |2003-10-01| ++ |23:57:59|,
  b: |2003-10-01| ++ |23:57:59Z|
}
Output
1
2
3
4
{
    "a": "2003-10-01T23:57:59",
    "b": "2003-10-01T23:57:59Z"
}

Note that the order in which the two objects are appended is irrelevant, so logically a 'Date' + 'Time' will result in the same as a '#Time' + 'Date'.

++(Time, Date): DateTime

You can append a date to a time (or localtime) object so as to provide a more precise value.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: |23:57:59| ++ |2003-10-01|,
  b: |23:57:59Z| ++ |2003-10-01|
}
Output
1
2
3
4
{
    "a": "2003-10-01T23:57:59",
    "b": "2003-10-01T23:57:59Z"
}

Note that the order in which the two objects are appended is irrelevant, so logically a 'Date' + 'Time' will result in the same as a '#Time' + 'Date'.

++(Date, TimeZone): DateTime

Appends a TimeZone to a Date type value and returns a DateTime result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |2003-10-01T23:57:59| ++ |-03:00|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

++(TimeZone, Date): DateTime

Appends a Date to a TimeZone type value and returns a DateTime result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |-03:00| ++ |2003-10-01T23:57:59|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

++(LocalDateTime, TimeZone): DateTime

Appends a TimeZone to a LocalDateTime type value and returns a DateTime result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |2003-10-01T23:57:59| ++ |-03:00|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

++(TimeZone, LocalDateTime): DateTime

Appends a LocalDateTime to a TimeZone type value and returns a DateTime result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |-03:00| ++ |2003-10-01T23:57:59|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

++(LocalTime, TimeZone): Time

Appends a TimeZone to a LocalTime type value and returns a Time result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |2003-10-01T23:57:59| ++ |-03:00|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

++(TimeZone, LocalTime): Time

Appends a LocalTime to a TimeZone type value and returns a Time result.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: |-03:00| ++ |2003-10-01T23:57:59|
Output
1
2
3
{
  "a": "2003-10-01T23:57:59-03:00"
}

 — 

--(Array<S>, Array<Any>): Array<S>

Returns a new array that removes every occurrence of elements listed in the right-hand side (rhs) array from the left-hand side (lhs) array. The result is that same as iteratively taking lhs - elementN, for each elementN in rhs.

When an element in the lhs array matches one of the values in the rhs array, it is removed. If multiple elements in the lhs array match a value, all matching values are removed from the lhs.

Transform
1
2
3
4
%dw 2.0
output application/json
---
a: [0, 1, 1, 2] -- [1,2]
Output
1
2
3
{
  "a": [0],
}

--({ (K)?: V }, Object): { (K)?: V }

Removes all the entries from the source that are present on the toRemove parameter .Transform

1
2
3
4
5
6
7
8
%dw 2.0
output application/json

---
{
   hello: 'world',
   name: "DW"
 } -- {hello: 'world'}
Output
1
2
3
{
   "name": "DW"
}

--(Object, Array<String>)

Removes the properties from the source that are present the given list of keys. .Transform

1
2
3
4
5
6
7
8
%dw 2.0
output application/json

---
{
   hello: 'world',
   name: "DW"
 } -- ['hello']
Output
1
2
3
{
   "name": "DW"
}

--(Object, Array<Key>)

Removes the properties from the source that are present the given list of keys. .Transform

1
2
3
4
5
6
7
8
%dw 2.0
output application/json

---
{
   hello: 'world',
   name: "DW"
 } -- ['hello' as Key]
Output
1
2
3
{
   "name": "DW"
}

abs

abs(Number): Number

Returns the absolute value of a number.

Transform
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---
{
  a: abs(-2),
  b: abs(2.5),
  c: abs(-3.4),
  d: abs(3)
}
Output
1
2
3
4
5
6
{
  "a": 2,
  "b": 2.5,
  "c": 3.4,
  "d": 3
}

avg

avg(Array<Number>): Number

Creates an average of all the values in an array and outputs a single number. The array must of course contain only numerical value in it.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: avg([1, 1000]),
  b: avg([1, 2, 3])
}
Output
1
2
3
4
{
  "a": 500.5,
  "b": 2.0
}

ceil

ceil(Number): Number

Rounds a number upwards, returning the first full number above than the one provided.

Transform
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/json
---

{
  a: ceil(1.5),
  b: ceil(2.2),
  c: ceil(3)
}
Output
1
2
3
4
5
{
  "a": 2,
  "b": 3,
  "c": 3
}

contains

contains(Array<T>, Any): Boolean

You can evaluate if any value in an array matches a given condition:

Transform
1
2
3
4
%dw 2.0
output application/json
---
ContainsRequestedItem: payload.root.*order.*items contains "3"
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <order>
      <items>155</items>
    </order>
    <order>
      <items>30</items>
    </order>
    <order>
      <items>15</items>
    </order>
    <order>
      <items>5</items>
    </order>
    <order>
      <items>4</items>
      <items>7</items>
    </order>
    <order>
      <items>1</items>
      <items>3</items>
    </order>
    <order>
        null
    </order>
</root>
Output
1
2
3
{
  "ContainsRequestedItem": true
}

contains(String, String): Boolean

You can also use contains to evaluate a substring from a larger string:

Transform
1
2
3
4
%dw 2.0
output application/json
---
ContainsString: payload.root.mystring contains "me"
Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <mystring>some string</mystring>
</root>
Output
1
2
3
{
  "ContainsString": true
}

contains(String, Regex): Boolean

Instead of searching for a literal substring, you can also match it against a regular expression:

Transform
1
2
3
4
%dw 2.0
output application/json
---
ContainsString: payload.root.mystring contains /s[t|p]ring/
Input
1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <mystring>A very long string</mystring>
</root>
Output
1
2
3
{
  "ContainsString": true
}

daysBetween

daysBetween(Date, Date): Number

Returns the number of days between two dates.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  "days": daysBetween("2016-10-01T23:57:59-03:00", "2017-10-01T23:57:59-03:00")
}
Output
1
2
3
 {
   "days": 365
 }

distinctBy

distinctBy(Array<T>, (item: T, index: Number) → Any): Array<T>

Returns only unique values from an array that may have duplicates. The lambda is invoked with two parameters: value and index. If these parameters are not defined, the index is defined by default as $$ and the value as $.

Transform
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
output application/json
---
{

    book : {
      title : payload.title,
      year: payload.year,
      authors: payload.author distinctBy $
    }
}
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "title": "XQuery Kick Start",
  "author": [
    "James McGovern",
    "Per Bothner",
    "Kurt Cagle",
    "James Linn",
    "Kurt Cagle",
    "Kurt Cagle",
    "Kurt Cagle",
    "Vaidyanathan Nagarajan"
  ],
  "year":"2000"
}
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({ (K)?: V }, (value: V, key: K) → Any): Object

Returns an object with unike key value pairs . The lambda is invoked with two parameters: value and key. If these parameters are not defined, the index is defined by default as $$ and the value as $.

Transform
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/xml
---
{

     book : {
        title : payload.book.title,
        authors: payload.book.&author distinctBy $
     }
}
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<book>
  <title> "XQuery Kick Start"</title>
  <author>
    James Linn
  </author>
  <author>
    Per Bothner
  </author>
  <author>
    James McGovern
  </author>
  <author>
    James McGovern
  </author>
  <author>
    James McGovern
  </author>
</book>
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<book>
  <title> "XQuery Kick Start"</title>
  <authors>
      <author>
        James Linn
      </author>
      <author>
        Per Bothner
      </author>
      <author>
        James McGovern
      </author>
  </authors>
</book>

endsWith

endsWith(String, String): String

Returns true or false depending on if a string ends with a provided substring.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: "Mariano" endsWith "no",
  b: "Mariano" endsWith "to"
}
Output
1
2
3
4
{
  "a": true,
  "b": false
}

filter

filter(Array<T>, (item: T, index: Number) → Boolean): Array<T>

Returns an array that only contains those elements that pass the criteria specified in the lambda. The lambda is invoked with two parameters: value and the index. If these parameters are not named, the index is defined by default as $$ and the value as $.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  biggerThanTwo: [0, 1, 2, 3, 4, 5] filter $ > 2
}
Output
1
2
3
{
  "biggerThanTwo": [3,4,5]
}

The next example passes named key and value parameters. .Transform

1
2
3
4
5
6
%dw 2.0
output application/json
---
{
 example2: [0, 1, 2, 3, 4, 5] filter ((key1, value1) -> key1 > 3 and value1 < 5 )
}
Output
1
2
3
{
  "example2": [4]
}

filter(Null, (item: Nothing, index: Nothing) → Boolean): Null

Helper function that allows filter to work with null values

filterObject

filterObject({ (K)?: V }, (value: V, key: K, index: Number) → Boolean): { (K)?: V }

Returns an object that filters an input object based on a matching condition. The lambda is invoked with three parameters: value, key and index. If these parameters are not named, the value is defined by default as $, the key * and the index *$.

This example filters an object by its value.

Transform
1
2
3
4
%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b"} filterObject ((value1) -> value1 == "a")
Output
1
2
3
{
  "letter1": "a"
}

You can produce the same results with this input:

Transform
1
2
3
4
%dw 2.0
output application/json
---
{"letter1": "a", "letter2": "b"} filter ($ == "a")

filterObject(Null, (value: Nothing, key: Nothing, index: Nothing) → Boolean): Null

Helper function that allows filterObject to work with null values

find

find(Array<T>, Any): Array<Number>

Returns the array of index where the element to be found where present

Transform
1
2
3
4
%dw 2.0
output application/json
---
["name", "lastName"] find "name"
Output
1
2
3
[
   0
]

find(String, Regex): Array<Array<Number>>

Returns the array of index where the regex matched in the text

Transform
1
2
3
4
%dw 2.0
output application/json
---
"DataWeave" find /a/
Output
1
2
3
[
   [1], [3], [6]
]

find(String, String): Array<Number>

Given a string, it returns the index position within the string at which a match was matched. If found in multiple parts of the string, it returns an array with the various idex positions at which it was found. You can either look for a simple string or a regular expression.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: "aabccde" find /(a).(b)(c.)d/,
  b: "aabccdbce" find "a",
  c: "aabccdbce" find "bc"
}
Output
1
2
3
4
5
{
  "a": [[0,0,2,3]],
  "b": [0,1],
  "c": [2,6]
}

flatMap

flatMap(Array<T>, (item: T, index: Number) → Array<R>): Array<R>

Maps the array of items using the specified callback and it will apply flatten to the result. .Transform

1
2
3
4
%dw 2.0
output application/json
---
users: ["john", "peter", "matt"] flatMap  [$$ as String, $]
Output
1
2
3
4
5
6
7
8
9
10
{
   "users": [
     "0",
     "john",
     "1",
     "peter",
     "2",
     "matt"
   ]
 }

flatMap(Null, (Nothing, Nothing) → Boolean): Null

Helper function that allows flatMap to work with null values

flatten

flatten(Array<Array<T> | Q>): Array<T | Q>

If you have an array of arrays, this operator can flatten it into a single simple array.

Transform
1
2
3
4
%dw 2.0
output application/json
---
flatten(payload)
Input
1
2
3
4
5
[
   [3,5],
   [9,5],
   [154,0.3]
]
Output
1
2
3
4
5
6
7
8
[
  3,
  5,
  9,
  5,
  154,
  0.3
]

floor

floor(Number): Number

Rounds a number downwards, returning the first full number below than the one provided.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: floor(1.5),
  b: floor(2.2),
  c: floor(3)
}
Output
1
2
3
4
5
{
  "a": 1,
  "b": 2,
  "c": 3
}

groupBy

groupBy(Array<T>, (item: T, index: Number) → R): { ®: Array<T> }

Partitions an Array into a Object that contains Arrays, according to the discriminator lambda you define. The lambda is invoked with three parameters: value, key and index. If these parameters are not named, the value is defined by default as $, the key * and the index *$.

Transform
1
2
3
4
%dw 2.0
output application/json
---
"language": payload.langs groupBy $.language
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "langs": [
    {
      "name": "Foo",
      "language": "Java"
    },
    {
      "name": "Bar",
      "language": "Scala"
    },
    {
      "name": "FooBar",
      "language": "Java"
    }
  ]
}
Output
1
2
3
4
5
6
7
8
9
10
11
{
  "language": {
    "Scala": [
        {"name":"Bar", "language":"Scala"}
      ],
    "Java": [
        {"name":"Foo", "language":"Java"},
        {"name":"FooBar", "language":"Java"}
      ]
  }
}

groupBy({ (K)?: V }, (value: V, key: K) → R): { ®: Array<T> }

Partitions an Object into a Object that contains Arrays, according to the discriminator lambda you define. The lambda is invoked with two parameters: value and the key.

groupBy(Null, (Nothing, Nothing) → Any): Null

Helper function that allows groupBy to work with null values

isBlank

isBlank(String): Boolean

Returns true if it receives a string composed of only whitespace characters.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output  application/json
---
{
  empty: isBlank(""),
  withSpaces: isBlank("      "),
  withText: isBlank(" 1223")
}
Output
1
2
3
4
5
  {
    "empty": true,
    "withSpaces": true,
    "withText": false
  }

isDecimal

isDecimal(Number): Boolean

Returns true if if receives a number that has any decimals in it.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  decimal: isDecimal(1.1),
  integer: isDecimal(1)
}
Output
1
2
3
4
  {
    "decimal": true,
    "integer": false
  }

isEmpty

isEmpty(Array<Any>): Boolean

Returns wether an Array is empty or not.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  empty: isEmpty([]),
  nonEmpty: isEmpty([1])
}
Output
1
2
3
4
  {
    "empty": true,
    "nonEmpty": false
  }

isEmpty(String): Boolean

Returns wether a String is empty or not.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  empty: isEmpty(""),
  nonEmpty: isEmpty("DataWeave")
}
Output
1
2
3
4
  {
    "empty": true,
    "nonEmpty": false
  }

isEmpty(Object): Boolean

Returns whether an Object is empty or not.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  empty: isEmpty({}),
  nonEmpty: isEmpty({name: "DataWeave"})
}
Output
1
2
3
4
  {
    "empty": true,
    "nonEmpty": false
  }

isEven

isEven(Number): Boolean

Returns true if the specified number is Even.

isInteger

isInteger(Number): Boolean

Returns true is the number doesn’t have any decimals.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  decimal: isInteger(1.1),
  integer: isInteger(1)
}
Output
1
2
3
4
  {
    "decimal": false,
    "integer": true
  }

isLeapYear

isLeapYear(DateTime): Boolean

Returns true if it receives a DateTime for a leap year.

isLeapYear(Date): Boolean

Returns true if it receives a Date for a leap year.

isLeapYear(LocalDateTime): Boolean

Returns true if it receives a LocalDateTime for a leap year.

isOdd

isOdd(Number): Boolean

Returns true if the specified number is Odd.

joinBy

joinBy(Array<Any>, String): String

Merges an array into a single string value, using the provided string as a separator between elements.

Transform
1
2
3
4
%dw 2.0
output application/json
---
aa: ["a","b","c"] joinBy "-"
Output
1
2
3
{
  "aa": "a-b-c"
}

log

log(String, T): T

Logs the specified value with the specified prefix, it then returns the value unchanged.

Example:
1
2
3
4
5
%dw 2.0
in payload application/json
output application/xml
---
 { age: log("My Age", payload.age) }
Input:
1
{ "age" : 33 }

This will print output: My Age - 33 .Output:

1
<age>33</age>

Note that besides producing the expected output, it also logs it.

lower

lower(String): String

Returns the provided string in lowercase characters.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  name: lower("MULESOFT")
}
Output
1
2
3
{
  "name": "mulesoft"
}

map

map(Array<T>, (item: T, index: Number) → R): Array<R>

Returns an array that is the result of applying a transformation function (lambda) to each of the elements. The lambda is invoked with two parameters: value and the index. If these parameters are not named, the index is defined by default as $$ and the value as $.

Transform
1
2
3
4
%dw 2.0
output application/json
---
users: ["john", "peter", "matt"] map  upper($)
Output
1
2
3
4
5
6
7
{
 "users": [
   "JOHN",
   "PETER",
   "MATT"
  ]
}

In the following example, custom names are defined for the index and value parameters of the map operation, and then both are used to construct the returned value. In this case, value is defined as firstName and its index in the array is defined as position.

Transform
1
2
3
4
%dw 2.0
output application/json
---
users: ["john", "peter", "matt"] map ((firstName, position) -> position ++ ":" ++ upper(firstName))
Output
1
2
3
4
5
6
7
{
  "users": [
    "0:JOHN",
    "1:PETER",
    "2:MATT"
  ]
}

map(Null, (Nothing, Nothing) → Boolean): Null

Helper function that allows map to work with null values

mapObject

mapObject({ (K)?: V }, (V, K, Number) → Object): Object

Similar to Map, but instead of processing only the values of an object, it processes both keys and values as a tuple. Also instead of returning an array with the results of processing these values through the lambda, it returns an object, which consists of a list of the key:value pairs that result from processing both key and value of the object through the lambda.

The lambda is invoked with three parameters: value, key and index. If these parameters are not named, the value is defined by default as $, the key * and the index *$.

Transform
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
var conversionRate=13.45
---
priceList: payload.prices mapObject (
  '$$':{
    dollars: $,
    localCurrency: $ * conversionRate
  }
)
Input
1
2
3
4
5
<prices>
    <basic>9.99</basic>
    <premium>53</premium>
    <vip>398.99</vip>
</prices>
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "priceList": {
    "basic": {
      "dollars": "9.99",
      "localCurrency": 134.3655
    },
    "premium": {
      "dollars": "53",
      "localCurrency": 712.85
    },
    "vip": {
      "dollars": "398.99",
      "localCurrency": 5366.4155
    }
  }
}
Tip
Note that when you use a parameter to populate one of the keys of your output, as with the case of in this example, you must either enclose it in quote marks or brackets. '' or ($$) are both equally valid.

In the example above, as key and value are not defined, they’re identified by the placeholders $$ and $. For each key:value pair in the input, the key is preserved and the value becomes an object with two properties: one of these is the original value, the other is the result of multiplying this value by a constant that is defined as a directive in the header.

The mapping below performs exactly the same transform, but it defines custom names for the properties of the operation, instead of using $ and $$. Here, 'category' is defined as referring to the original key in the object, and 'money' to the value in that key.

Transform
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
var conversionRate=13.45
---
priceList: payload.prices mapObject ((money, category, index) ->
  '$category':{
    dollars: money,
    localCurrency: money * conversionRate
  }
)
Tip
Note that when you use a parameter to populate one of the keys of your output, as with the case of category in this example, you must either enclose it in brackets or enclose it in quote marks adding a $ to it, otherwise the name of the property is taken as a literal string. '$category' or (category) are both equally valid.

mapObject(Null, (Any, Any, Number) → Any): Null

Helper function that allows mapObject to work with null values

match

match(String, Regex): Array<String>

Matches a string against a regular expression. It returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex.

It can be applied to the result of any evaluated expression, and can return any evaluated expression. See the Match operator in the DataWeave Language Introduction.

Transform
1
2
3
4
%dw 2.0
output application/json
---
hello: "anniepoint@mulesoft.com" match /([a-z]*)@([a-z]*).com/
Output
1
2
3
4
5
6
7
{
  "hello": [
    "anniepoint@mulesoft.com",
    "anniepoint",
    "mulesoft"
  ]
}

In the example above, we see that the search regular expression describes an email address. It contains two capture groups, what’s before and what’s 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.

matches

matches(String, Regex): Boolean

Matches a string against a regular expression, and returns true or false.

Transform
1
2
3
4
%dw 2.0
output application/json
---
b: "admin123" matches /(\d+)/
Output
1
2
3
{
  "b": false
}
Tip
For more advanced use cases where you need to output or conditionally process the matched value, see Pattern Matching in DataWeave.

max

max(Array<T>): T | Null

Returns the highest element in an array. Returns null when the array is empty

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: max([1, 1000]),
  b: max([1, 2, 3]),
  d: max([1.5, 2.5, 3.5])
}
Output
1
2
3
4
5
{
  "a": 1000,
  "b": 3,
  "d": 3.5
}

maxBy

maxBy(Array<T>, (item: T) → Comparable): T | Null

Returns the element used to get the maximum result using a function. Return null when array is empty

Transform
1
2
3
4
%dw 2.0
output  application/json
---
[ { a: "1" }, { a: "2" }, { a: "3" } ] maxBy ((item) -> item.a as Number)
Output
1
{ "a": "3" }

min

min(Array<T>): T | Null

Returns the lowest element in an array. Returns null when the array is empty

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: min([1, 1000]),
  b: min([1, 2, 3]),
  d: min([1.5, 2.5, 3.5])
}
Output
1
2
3
4
5
{
  "a": 1,
  "b": 1,
  "d": 1.5
}

minBy

minBy(Array<T>, (item: T) → Comparable): T | Null

Returns the element used to get the minimum result using a function. Return null when array is empty

Transform
1
2
3
4
%dw 2.0
output  application/json
---
[ { a: 1 }, { a: 2 }, { a: 3 } ] minBy (item) -> item.a
Output
1
{ "a": 1 }

mod

mod(Number, Number): Number

Returns the remainder after performing a division of the first number by the second one.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: 3 mod 2,
  b: 4 mod 2,
  c: 2.2 mod 2
}
Output
1
2
3
4
5
{
  "a": 1,
  "b": 0,
  "c": 0.2
}

native

native(String): Nothing

Loads a native function using the specified identifier.

now

now(): DateTime

Returns a (Datetime) object with the current date and time.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: now(),
  b: now().day,
  c: now().minutes
}
Output
1
2
3
4
5
{
  "a": "2015-12-04T18:15:04.091Z",
  "b": 4,
  "c": 15
}
Tip
See DataWeave Selectors for a list of possible selectors to use here.

orderBy

orderBy(O, (V, K) → R): O

Returns the provided array (or object) ordered according to the value returned by the lambda. The lambda is invoked with two parameters: value and the index. If these parameters are not named, the index is defined by default as $$ and the value as $.

Transform
1
2
3
4
%dw 2.0
output application/json
---
orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy $.letter
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "orderByLetter": [
    {
      "letter": "a"
    },
    {
      "letter": "b"
    },
    {
      "letter": "c"
    },
    {
      "letter": "d"
    },
    {
      "letter": "e"
    }
  ]
}
Tip

The orderBy function doesn’t have an option to order in descending order instead of ascending. What you can do in these cases is simply invert the order of the resulting array.

Transform
1
2
3
4
%dw 2.0
output application/json
---
orderDescending: ([3,8,1] orderBy -$)
Output
1
{ "orderDescending": [8,3,1] }

orderBy(Array<T>, (T, Number) → R): Array<T>

Sorts the array using the specified criteria

Transform
1
2
3
4
5
%dw 2.0
 in payload application/json
 output application/json
 ---
 [3,2,3] orderBy $
Output
1
2
3
4
5
[
  2,
  3,
  3
]

pluck

pluck({ (K)?: V }, (V, K, Number) → R): Array<R>

Pluck is useful for mapping an object into an array. Pluck is an alternate mapping mechanism to mapObject. Like mapObject, pluck executes a lambda over every key:value pair in its processed object as a tuple, but instead of returning an object, it returns an array, which may be built from either the values or the keys in the object.

The lambda is invoked with three parameters: value, key and index. If these parameters are not named, the value is defined by default as $, the key * and the index *$.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
result: {
  keys: payload.prices pluck $$,
  values: payload.prices pluck $
}
Input
1
2
3
4
5
<prices>
    <basic>9.99</basic>
    <premium>53</premium>
    <vip>398.99</vip>
</prices>
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "result": {
    "keys": [
      "basic",
      "premium",
      "vip"
    ],
    "values": [
      "9.99",
      "53",
      "398.99"
    ]
  }
}

pluck(Null, (Nothing, Nothing, Nothing) → Any): Null

Helper function that allows pluck to work with null values

pow

pow(Number, Number): Number

Returns the result of the first number a to the power of the number following the pow operator.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: 2 pow 3,
  b: 3 pow 2,
  c: 7 pow 3
}
Output
1
2
3
4
5
{
  "a": 8,
  "b": 9,
  "c": 343
}

random

random(): Number

Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  price: random() * 1000
}

randomInt

randomInt(Number): Number

Returns a pseudo-random integer number between 0 and the specified number (exclusive).

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  price: randomInt(1000) //Returns an integer from 0 to 1000
}

read

read(String | Binary, String, Object)

The read function returns the result of parsing the content parameter with the specified mimeType reader.

The first argument points the content that must be read, the second is the format in which to write it. A third optional argument lists reader configuration properties.

Example:
1
2
3
4
 %dw 2.0
 output application/xml
 ---
 read('{"name":"DataWeave"}', "application/json")
Output:
1
 <name>DataWeave</name>

readUrl

readUrl(String, String, Object)

Same as the read operator, but using a URL as the content provider.

reduce

reduce(Array<T>, (item: T, acumulator: T) → T): T | Null

Apply a reduction to the array using just two parameters: the accumulator ($$), and the value ($). By default, the accumulator starts at the first value of the array. If the array is empty and no default value was set to the accumulator then null value is returned

Transform
1
2
3
4
%dw 2.0
output application/json
---
sum: [0, 1, 2, 3, 4, 5] reduce ($$ + $)
Output
1
2
3
{
  "sum": 15
}
Transform
1
2
3
4
%dw 2.0
output application/json
---
concat: ["a", "b", "c", "d"] reduce ($$ ++ $)
Output
1
2
3
{
  "concat": "abcd"
}

In some cases, you may not want to use the first element of the array as an accumulator. To set the accumulator to something else, you must define this in a lambda.

Transform
1
2
3
4
%dw 2.0
output application/json
---
concat: ["a", "b", "c", "d"] reduce ((val, acc = "z") -> acc ++ val)
Output
1
2
3
{
  "concat": "zabcd"
}

In other cases, you may want to turn an array into a string keeping the commas in between. The example below defines a lambda that also adds commas when concatenating.

Transform
1
2
3
4
%dw 2.0
output application/json
---
concat: ["a", "b", "c", "d"] reduce ((val, acc) -> acc ++ "," ++ val)
Output
1
2
3
{
  "concat":  "a,b,c,d"
}

reduce(Array<T>, (item: T, acumulator: A) → A): A

replace

replace(String, Regex): ((Array<String>, Number) → String) → String

Replaces a section of a string for another, in accordance to a regular expression, and returns a modified string.

Transform
1
2
3
4
%dw 2.0
output application/json
---
b: "admin123" replace /(\d+)/ with "ID"
Output
1
2
3
{
  "b": "adminID"
}

replace(String, String): ((Array<String>, Number) → String) → String

Replaces the occurance of a given string inside other string with the specified value

Transform
1
2
3
4
%dw 2.0
output application/json
---
b: "admin123" replace "123" with "ID"
Output
1
2
3
{
  "b": "adminID"
}

round

round(Number): Number

Rounds the value of a number to the nearest integer.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: round(1.2),
  b: round(4.6),
  c: round(3.5)
}
Output
1
2
3
4
5
{
  "a": 1,
  "b": 5,
  "c": 4
}

scan

scan(String, Regex): Array<Array<String>>

Returns an array with all of the matches in the given string. Each match is returned as an array that contains the complete match, as well as any capture groups there may be in your regular expression.

Transform
1
2
3
4
%dw 2.0
output application/json
---
hello: "anniepoint@mulesoft.com,max@mulesoft.com" scan /([a-z]*)@([a-z]*).com/
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "hello": [
    [
      "anniepoint@mulesoft.com",
      "anniepoint",
      "mulesoft"
    ],
    [
      "max@mulesoft.com",
      "max",
      "mulesoft"
    ]
  ]
}

In the example above, we see that the search regular expression describes an email address. It contains two capture groups, what’s before and what’s after the @. The result is an array with two matches, as there are two email addresses in the input string. Each of these matches 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.

sizeOf

sizeOf(Array<Any>): Number

Returns the number of elements in an array (or anything that can be converted to an array such as a string).

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  arraySize: sizeOf([1,2,3])
}
Output
1
2
3
{
  "arraySize": 3
}

sizeOf(Object): Number

Returns the number of elements in an object .

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  objectSize: sizeOf({a:1,b:2})
}
Output
1
2
3
{
  "objectSize": 2
}

sizeOf(Binary): Number

Returns the byte length of a binary value.

sizeOf(String): Number

Returns the number of characters in an string

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  textSize: sizeOf("MuleSoft")
}
Output
1
2
3
{
  "textSize": 8
}

splitBy

splitBy(String, Regex): Array<String>

Performs the opposite operation as Join By. It splits a string into an array of separate elements, looking for instances of the provided string and using it as a separator.

Transform
1
2
3
4
%dw 2.0
output application/json
---
split: "a-b-c" splitBy /-/
Output
1
2
3
{
  "split": ["a","b","c"]
}

splitBy(String, String): Array<String>

Performs the opposite operation as Join By. It splits a string into an array of separate elements, looking for instances of the provided string and using it as a separator.

Transform
1
2
3
4
%dw 2.0
output application/json
---
split: "a-b-c" splitBy "-"
Output
1
2
3
{
  "split": ["a","b","c"]
}

sqrt

sqrt(Number): Number

Returns the square root of the provided number.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: sqrt(4),
  b: sqrt(25),
  c: sqrt(100)
}
Output
1
2
3
4
5
{
  "a": 2.0,
  "b": 5.0,
  "c": 10.0
}

startsWith

startsWith(String, String): Boolean

Returns true or false depending on if a string starts with a provided substring.

Transform
1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
  a: "Mariano" startsWith "Mar",
  b: "Mariano" startsWith "Em"
}
Output
1
2
3
4
{
  "a": true,
  "b": false
}

sum

sum(Array<Number>): Number

Given an array of numbers, it returns the result of adding of all of them.

Transform
1
2
3
4
%dw 2.0
output application/json
---
sum([1, 2, 3])
Output
1
6

to

to(Number, Number): Range

Returns a range within the specified boundries. The upper boundry is inclusive.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
    "myRange": 1 to 10
}
Output
1
2
3
{
    "myRange": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

trim

trim(String): String

Removes any excess spaces at the start and end of a string.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  "a": trim("   my long text     ")
}
Output
1
2
3
{
  "a": "my long text"
}

typeOf

typeOf(T): Type<T>

Returns the type of a value.

Transform
1
2
3
4
%dw 2.0
output application/json
---
typeOf("A Text")
Output
1
"String"

unzip

unzip(Array<Array<T>>): Array<Array<T>>

Performs the opposite function of [zip arrays], that is: given a single array where each index contains an array with two elements, it outputs two separate arrays, each with one of the elements of the pair. This can also be scaled up, if the indexes in the provided array contain arrays with more than two elements, the output will contain as many arrays as there are elements for each index.

Transform
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
{
  a: unzip([[0,"a"],[1,"b"],[2,"c"],[3,"d"]]),
  b: unzip([ [0,"a"], [1,"a"], [2,"a"], [3,"a"]]),
  c: unzip([ [0,"a"], [1,"a","foo"], [2], [3,"a"]])
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
{
   "a":[
      [0, 1, 2, 3],
      ["a", "b", "c", "d"]
    ],
  "b": [
      [0,1,2,3],
      ["a","a","a","a"]
    ],
  "c": [
      [0,1,2,3]
    ]
}

Note even though example b can be considered the inverse function to the example b in [zip array], the result is not analogous, since it returns an array of repeated elements instead of a single element. Also note that in example c, since the number of elements in each component of the original array is not consistent, the output only creates as many full arrays as it can, in this case just one.

upper

upper(String): String

Returns the provided string in uppercase characters.

Transform
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  name: upper("mulesoft")
}
Output
1
2
3
{
  "name": "MULESOFT"
}

uuid

uuid(): String

Returns a v4 UUID using random numbers as the source.

with

with(((V, U) → R) → X, (V, U) → R): X

Used with the replace applies the specified function

write

write(Any, String, Object): Any

The write function returns a string with the serialized representation of the value in the specified mimeType.

The first argument points to the element that must be written, the second is the format in which to write it. A third optional argument lists writer configuration properties. See Mime Types Supported by DataWeave for a full list of available configuration options for each different format.

Transform
1
2
3
4
5
6
%dw 2.0
output application/xml
---
{
 output: write(payload, "application/csv", {"separator" : "|"})
}
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"
  }
]
Output
1
2
3
4
5
<?xml version='1.0' encoding='US-ASCII'?>
<output>Name|Email|Id|Title
Mr White|white@mulesoft.com|1234|Chief Java Prophet
Mr Orange|orange@mulesoft.com|4567|Integration Ninja
</output>

zip

zip(Array<T>, Array<R>): Array<Array<T | R>>

Given two or more separate lists, the zip function can be used to merge them together into a single list of consecutive n-tuples. Imagine two input lists each being one side of a zipper: similar to the interlocking teeth of a zipper, the zip function interdigitates each element from each input list, one element at a time.

Transform
1
2
3
4
5
6
7
8
%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"]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "a": [
    [0,"a"],
    [1,"b"],
    [2,"c"],
    [3,"d"]
    ],
  "b": [
    [0,"a"]
  ],
  "c": [
    [0,"a"],
    [1,"b"]
  ]
}

Here is another example of the zip function with more than two input lists.

Transform
1
2
3
4
%dw 2.0
output application/json
---
payload.list1 zip payload.list2 zip payload.list3
Input
1
2
3
4
5
6
{
  "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"]]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
  [
    "a",
    1,
    "aa"
  ],
  [
    "b",
    2,
    "bb"
  ],
  [
    "c",
    3,
    "cc"
  ]
]

Types

Any

Any type, is the top level type. Any extends all of the system types. That means anything can be assigned to a Any typed variable.

Definition
1
Any

Array

Array type, requires a Type(T) to represent the elements of the list. Example: Array<Number> represents an array of numbers.

Definition
1
Array

Binary

A Blob

Definition
1
Binary

Boolean

A Boolean type true of false

Definition
1
Boolean

CData

XML defines a custom type named CData, it 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.

Transform
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/xml
---
{
  users:
  {
    user : "Mariano" as CData,
    age : 31 as CData
  }
}
Output
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user><![CDATA[Mariano]]></user>
  <age><![CDATA[31]]></age>
</users>
Definition
1
String {cdata: true}

Comparable

A union type that represents all the types that can be compared to each other.

Definition
1
String | Number | Boolean | DateTime | LocalDateTime | Date | LocalTime | Time | TimeZone

Date

A Date represented by Year Month Day

Definition
1
Date

DateTime

A Date Time with in a TimeZone

Definition
1
DateTime

Dictionary

Generic Dictionary interface

Definition
1
{ _?: T }

Enum

This type is based in 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.

Transform
1
2
3
4
%dw 2.0
output application/java
---
"Male" as Enum {class: "com.acme.GenderEnum"}
Definition
1
String {enumeration: true}

Iterator

This type is based in the iterator Java class. The iterator contains a collection, and includes methods to iterate through and filter it.

Note
Just like the Java class, the iterator is designed to be consumed only once. For example, if you then pass this value to a logger would result in consuming it and it would no longer be readable to further elements in the flow.
Definition
1
Array {iterator: true}

Key

A Key of an Object

Definition
1
Key

LocalDateTime

A DateTime in the current TimeZone

Definition
1
LocalDateTime

LocalTime

A Time in the current TimeZone

Definition
1
LocalTime

Namespace

A namespace type represented by an Uri and a Prefix

Definition
1
Namespace

Nothing

Bottom type. This type is can be assigned to all the types

Definition
1
Nothing

Null

A null type

Definition
1
Null

Number

A number any number decimals and intigers are represented by Number type

Definition
1
Number

Object

Object type. Represents any object, collection of Key Value Pairs

Definition
1
Object

Period

A Period

Definition
1
Period

Range

A Range type represents a sequence of numbers

Definition
1
Range

Regex

Regex Type

Definition
1
Regex

SimpleType

A union type that represents all the simple types.

Definition
1
String | Boolean | Number | DateTime | LocalDateTime | Date | LocalTime | Time | TimeZone | Period

String

String type

Definition
1
String

Time

A Time in a specific TimeZone

Definition
1
Time

TimeZone

A TimeZone

Definition
1
TimeZone

Type

Represents a Type in the DataWeave Type System

Definition
1
Type

Uri

An Uri

Definition
1
Uri