Functions

toBinary

toBinary(String, String): Binary

Transforms a String into a Binary using the given encoding

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

encoding

Example

This example shows how the toBinary behaves under different inputs.

Source
1
2
3
4
5
6
7
%dw 2.0
import toBinary from dw::util::Coercions
output application/dw
---
{
  a: toBinary("DW Binary", "UTF-32"),
}
Output
1

toBoolean

toBoolean(String): Boolean

Transform a String into a Boolean

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to be transformed

Example

This example shows how the toBoolean behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
import toBoolean from dw::util::Coercions
---
{
  a: toBoolean("true"),
  b: toBoolean("false"),
  c: toBoolean("FALSE"),
  d: toBoolean("TrUe"),
}
Output
1
2
3
4
5
6
{
   "a": true,
   "b": false,
   "c": false,
   "d": true
 }

toDate

toDate(String, String | Null, String | Null): Date

Transforms a String into a Date using the given format

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to transform

format

The format to be used

locale

The locale of the format

Example

This example shows how the toDate behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import toDate from dw::util::Coercions
output application/dw
---
{

  a: toDate("2015-10-01"),
  b: toDate("2003/10/01","yyyy/MM/dd")
}
Output
1
2
3
4
{
   a: |2015-10-01|,
   b: |2003-10-01| as Date {format: "yyyy/MM/dd"}
 }

toDateTime

toDateTime(Number, MillisOrSecs | Null): DateTime

Transform the given number into a DateTime using the given unit :"milliseconds" or "seconds"

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

number

The number of millis or secs

unit

The unit if null "seconds" is the default

Example

This example shows how the toDateTime behaves under different inputs.

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

import toDateTime from dw::util::Coercions
---
{
    fromEpoch: toDateTime(1443743879),
    fromMillis: toDateTime(1443743879000, "milliseconds"),
}
Output
1
2
3
4
 {
     fromEpoch: |2015-10-01T23:57:59Z|,
     fromMillis: |2015-10-01T23:57:59Z|
 }

toDateTime(String, String | Null, String | Null): DateTime

Transforms a String into a DateTime using the given format

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to transform

format

The format to be used

locale

The locale of the format

Example

This example shows how the toDateTime behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import toDateTime from dw::util::Coercions
output application/dw
---
{
   a: toDateTime("2015-10-01T23:57:59Z"),
   b: toDateTime("2003-10-01 23:57:59Z","yyyy-MM-dd HH:mm:ssz"),
}
Output
1
2
3
4
{
   a: |2015-10-01T23:57:59Z|,
   b: |2003-10-01T23:57:59Z| as DateTime {format: "yyyy-MM-dd HH:mm:ssz"},
 }

toLocalDateTime

toLocalDateTime(String, String | Null, String | Null): LocalDateTime

Transforms a String into a LocalDateTime using the given format

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to transform

format

The format to be used

locale

The locale of the format

Example

This example shows how the toLocalDateTime behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import toLocalDateTime from dw::util::Coercions
output application/dw
---
{

  a: toLocalDateTime("2015-10-01T23:57:59"),
  b: toLocalDateTime("2003-10-01 23:57:59","yyyy-MM-dd HH:mm:ss")
}
Output
1
2
3
4
{
   a: |2015-10-01T23:57:59|,
   b: |2003-10-01T23:57:59| as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss"}
}

toLocalTime

toLocalTime(String, String | Null, String | Null): LocalTime

Transforms a String into a LocalTime using the given format

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to transform

format

The format to be used

locale

The locale of the format

Example

This example shows how the toLocalTime behaves under different inputs.

Source
1
2
3
4
5
6
7
%dw 2.0
import toLocalTime from dw::util::Coercions
output application/dw
---
{
   a: toLocalTime("23:57:59"),
}
Output
1
2
3
{
     a: |23:57:59|
}

toNumber

toNumber(DateTime, MillisOrSecs | Null): Number

Transforms a DateTime into a the Number of seconds or milliseconds depending in the specified unit.

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

dateTime

The datetime to be used

unit

The unit ("milliseconds"

Example

This example shows how the toNumber behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import toNumber from dw::util::Coercions
output application/json
---
{
    epoch: toNumber(|2015-10-01T23:57:59Z|),
    millis: toNumber(|2015-10-01T23:57:59Z|, "milliseconds"),
}
Output
1
2
3
4
 {
    epoch: 1443743879,
    millis: 1443743879000
 }

toNumber(Period, PeriodUnits | Null): Number

Transforms the given Period into a Number using the specified unit : "hours", "minutes" , "seconds" , "milliseconds" or "nanos"

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

period

The period to be converted

unit

The unit to be used

Example

This example shows how the toNumber behaves under different inputs.

Source
1
2
3
4
5
6
7
%dw 2.0
import toNumber from dw::util::Coercions
output application/json
---
{
    toSeconds: toNumber(|PT1H10M|, "seconds")
}
Output
1
2
3
 {
  "toSeconds": 4200
 }

toNumber(String | Key, String | Null, String | Null): Number

Transforms the specified String into a Number.

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

value

format

locale

Example

This example shows how the toNumber behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import toNumber from dw::util::Coercions
output application/json
---
 {
     "default": toNumber("1.0"),
     withFormat: toNumber("0.005",".00"),
     withLocale: toNumber("0,035","#.##","ES"),
 }
Output
1
2
3
4
5
{
   "default": 1.0,
   "withFormat": 0.005,
   "withLocale": 0.035
}

toPeriod

toPeriod(String): Period

Transform a String into a Period

Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later.

Parameters
Name Description

str

The string to be transformed

Example

This example shows how the toPeriod behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import toPeriod from dw::util::Coercions
output application/dw
---
{
  a: toPeriod("P1D"),
  b: toPeriod("PT1H1M"),
}
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
{
   a: |P1D|,
   b: |PT1H1M|
 }
* ----


=== toRegex

==== toRegex(String): Regex

Transforms a String into a Regex

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| str | The string to be transformed
|===

===== Example

This example shows how the `toRegex` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 output application/dw import toRegex from dw::util::Coercions --- { a: toRegex("a-Z"), b: toRegex("0-9+") }

====== Output

[source,DataWeave,linenums]

{ a: /a-Z/, b: /0-9+/ }

=== toString

==== toString(Number, String | Null, String | Null, RoundingMode | Null): String

Formats a Number and return a String with the result.
A Pattern can be used in order to specify how the number needs to be formatted.

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| number | The number to be formatted
| format | The format to be used
| locale | The locale
| roundMode | "UP" or "DOWN" or "CEILING" or "FLOOR" or "HALF_UP" or "HALF_DOWN" or "HALF_EVEN"
|===

===== Example

This example shows how the `toString` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 import toString from dw::util::Coercions output application/json --- { a: toString(1.0), b: toString(0.005,".00"), c: toString(0.035,".","ES"), d: toString(0.005,".","ES","HALF_EVEN"), e: toString(0.035,"#.00",null,"HALF_EVEN"), }

====== Output

[source,Json,linenums]

{ "a": "1", "b": ".01", "c": "0,04", "d": "0", "e": ".04" }

==== toString(Date | DateTime | LocalDateTime | LocalTime | Time, String | Null, String | Null): String

Formats a Date DateTime Time into a String.

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| date   | The date to be formatted
| format | The format to be used to format the date. If null no format will be used
| locale | The locale to be used. If null no locale will be used
|===

===== Example

This example shows how the `toString` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 import toString from dw::util::Coercions output application/json --- { a: toString(|2015-10-01T23:57:59|), b: toString(|2003-10-01T23:57:59|, "yyyy-MM-dd HH:mm:ss a"), c: toString(|2003-10-01|, "yyyy/MM/dd"), d: toString(|23:57:59|, "HH-mm-ss"), e: toString(|2003-10-01T23:57:59|, "yyyy-MM-dd HH:mm:ss a", "ES"), }

====== Output

[source,Json,linenums]

{ "a": "2015-10-01T23:57:59", "b": "2003-10-01 23:57:59 PM", "c": "2003/10/01", "d": "23-57-59", "e": "2003-10-01 23:57:59 PM" }

==== toString(Binary, String): String

Transforms a Binary data into a String using the given encoding

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| binary | The binary data
| encoding | The encoding to be used
|===

===== Example

This example shows how the `toString` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 output application/json var binaryData= "DW Test" as Binary {encoding: "UTF-32"} --- { a: toString(binaryData, "UTF-32"), }

====== Output

[source,Json,linenums]

{ "a": "DW Test" }

==== toString(TimeZone | Uri | Boolean | Period | Regex | Key): String

Transform the value (TimeZone | Uri | Boolean| Period| Regex| Key) into the String representation

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| data | The data to be transformed
|===

===== Example

This example shows how the `toString` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 output application/json --- { a: toString(|Z|), b: toString(true), c: toString(|P1D|), d: toString(/a-Z/), }

====== Output

[source,Json,linenums]

{ "a": "Z", "b": "true", "c": "P1D", "d": "a-Z" }

=== toTime

==== toTime(String, String | Null, String | Null): Time

Transforms a String into a Time using the given format

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| str | The string to transform
| format | The format to be used
| locale | The locale of the format
|===

===== Example

This example shows how the `toTime` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 import toTime from dw::util::Coercions output application/dw --- { a: toTime("23:57:59Z"), }

====== Output

[source,DataWeave,linenums]

{ a: |23:57:59Z| }

=== toTimeZone

==== toTimeZone(String): TimeZone

Transform a String into a TimeZone

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| str | The string to be transformed
|===

===== Example

This example shows how the `toBoolean` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 output application/dw import toTimeZone from dw::util::Coercions --- { a: toTimeZone("-03:00"), b: toTimeZone("Z"), c: toTimeZone("America/Argentina/Buenos_Aires"), }

====== Output

[source,DataWeave,linenums]

{ a: |-03:00|, b: |Z|, c: |America/Argentina/Buenos_Aires| }

=== toUri

==== toUri(String): Uri

Transforms a String into a URI

_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._

===== Parameters

[%header, cols="1,3"]
|===
| Name   | Description
| str | The String to be transformed
|===

===== Example

This example shows how the `toUri` behaves under different inputs.

====== Source

[source,DataWeave,linenums]

%dw 2.0 output application/json import toUri from dw::util::Coercions --- { a: toUri("http://google.com") }

====== Output

[source,DataWeave,linenums]

{ a: "http://google.com" }

== Types

=== MillisOrSecs


.Definition

[source,DataWeave,linenums]

"milliseconds" | "seconds"

=== PeriodUnits


.Definition

[source,DataWeave,linenums]

"hours" | "minutes" | "seconds" | "milliseconds" | "nanos"

=== RoundingMode


.Definition

[source,DataWeave,linenums]

"UP" | "DOWN" | "CEILING" | "FLOOR" | "HALF_UP" | "HALF_DOWN" | "HALF_EVEN"