This utility module assists with type coercions.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::util::Coercions
to the header of your
DataWeave script.
Functions
toArray
toArray(@StreamCapable text: String): Array<String>
Splits a String
value into an Array
of characters.
Parameters
Name | Description |
---|---|
text |
The |
Example
This example shows how toArray
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/json indent=false
---
{
a: toArray(""),
b: toArray("hola")
}
Output
1
{"a": [],"b": ["h","o","l","a"]}
toBinary
toBinary(str: String, encoding: String): Binary
Transform a String
value into a Binary
value
using the specified encoding.
Parameters
Name | Description |
---|---|
str |
The |
encoding |
The encoding to apply to the |
Example
This example shows how toBinary
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
'UTF-16Ex': toBinary("DW", "UTF-16"),
'utf16Ex': toBinary("DW", "utf16"),
'UnicodeBigEx': toBinary("DW", "UnicodeBig"),
'UTF-32Ex': toBinary("DW", "UTF-32"),
'UTF_32Ex': toBinary("DW", "UTF_32")
}
Output
1
2
3
4
5
6
7
{
"UTF-16Ex": "/v8ARABX" as Binary {base: "64"},
utf16Ex: "/v8ARABX" as Binary {base: "64"},
UnicodeBigEx: "/v8ARABX" as Binary {base: "64"},
"UTF-32Ex": "AAAARAAAAFc=" as Binary {base: "64"},
UTF_32Ex: "AAAARAAAAFc=" as Binary {base: "64"}
}
toBoolean
toBoolean(str: String): Boolean
Transform a String
value into a Boolean
value.
Parameters
Name | Description |
---|---|
str |
The |
Example
This example shows how toBoolean
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
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(str: String, format: String | Null = null, locale: String | Null = null): Date
Transforms a String
value into a Date
value
and accepts a format and locale.
Parameters
Name | Description |
---|---|
str |
The |
format |
The formatting to use on the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toDate
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
a: toDate("2015-10-01"),
b: toDate("2003/10/01","uuuu/MM/dd")
}
Output
1
2
3
4
{
a: |2015-10-01|,
b: |2003-10-01| as Date {format: "uuuu/MM/dd"}
}
toDateTime
toDateTime(number: Number, unit: MillisOrSecs | Null = null): DateTime
Transforms a Number
value into a DateTime
value
using milliseconds
or seconds
as the unit.
Parameters
Name | Description |
---|---|
number |
The |
unit |
The unit to use for the conversion: |
Example
This example shows how toDateTime
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
fromEpoch: toDateTime(1443743879),
fromMillis: toDateTime(1443743879000, "milliseconds")
}
Output
1
2
3
4
{
fromEpoch: |2015-10-01T23:57:59Z|,
fromMillis: |2015-10-01T23:57:59Z| as DateTime {unit: "milliseconds"}
}
toDateTime(str: String, format: String | Null = null, locale: String | Null = null): DateTime
Transforms a String
value into a DateTime
value
and accepts a format and locale.
Parameters
Name | Description |
---|---|
str |
The |
format |
The formatting to use on the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toDateTime
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
a: toDateTime("2015-10-01T23:57:59Z"),
b: toDateTime("2003-10-01 23:57:59Z","uuuu-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: "uuuu-MM-dd HH:mm:ssz"}
}
toLocalDateTime
toLocalDateTime(str: String, format: String | Null = null, locale: String | Null = null): LocalDateTime
Transforms a String
value into a LocalDateTime
value
and accepts a format and locale.
Parameters
Name | Description |
---|---|
str |
The |
format |
The formatting to use on the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toLocalDateTime
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
a: toLocalDateTime("2015-10-01T23:57:59"),
b: toLocalDateTime("2003-10-01 23:57:59","uuuu-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: "uuuu-MM-dd HH:mm:ss"}
}
toLocalTime
toLocalTime(str: String, format: String | Null = null, locale: String | Null = null): LocalTime
Transforms a String
value into a LocalTime
value
and accepts a format and locale.
Parameters
Name | Description |
---|---|
str |
The |
format |
The formatting to use on the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toLocalTime
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
toLocalTimeEx: toLocalTime("23:57:59"),
toLocalTimeEx2: toLocalTime("13:44:12.283","HH:mm:ss.n")
}
Output
1
2
3
4
{
"toLocalTimeEx": "23:57:59",
"toLocalTimeEx2": "13:44:12.283"
}
toNumber
toNumber(dateTime: DateTime, unit: MillisOrSecs | Null = null): Number
A variant of toNumber
that transforms a DateTime
value
into a number of seconds or milliseconds, depending on the
selected unit.
Parameters
Name | Description |
---|---|
dateTime |
The |
unit |
The unit of time ( |
Example
This example shows how toNumber
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * 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: Period, unit: PeriodUnits | Null = null): Number
A variant of toNumber
that transforms a Period
value
into a number of hours, minutes, seconds, milliseconds
or nanoseconds (nanos
).
Parameters
Name | Description |
---|---|
period |
The |
unit |
The unit to apply to the specified |
Example
This example shows how toNumber
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
toSecondsEx1: toNumber(|PT1H10M|, "seconds"),
toSecondsEx2: toNumber(|PT1M7S|, "milliseconds")
}
Output
1
2
3
4
{
"toSecondsEx1": 4200,
"toSecondsEx2": 67000
}
toNumber(value: String | Key, format: String | Null = null, locale: String | Null = null): Number
A variant of toNumber
that transforms a String
or Key
value into
a Number
value and that accepts a format and locale.
Parameters
Name | Description |
---|---|
value |
The |
format |
Optional formatting to apply to the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toNumber
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * from dw::util::Coercions
var myKey = keysOf({"123" : "myValue"})
output application/json
---
{
"default": toNumber("1.0"),
"withFormat": toNumber("0.005",".00"),
"withLocal": toNumber("1,25","#.##","ES"),
"withExtraPlaceholders": toNumber("5.55","####.####"),
"keyToNumber": toNumber(myKey[0])
}
Output
1
2
3
4
5
6
7
{
"default": 1.0,
"withFormat": 0.005,
"withLocal": 1.25,
"withExtraPlaceholders": 5.55,
"keyToNumber": 123
}
toPeriod
toPeriod(str: String): Period
Transform a String
value into a Period
value.
Parameters
Name | Description |
---|---|
str |
The |
Example
This example shows how toPeriod
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
toPeriodEx1: toPeriod("P1D"),
toPeriodEx2: toPeriod("PT1H1M")
}
Output
1
2
3
4
{
toPeriodEx1: |P1D|,
toPeriodEx2: |PT1H1M|
}
toRegex
toRegex(str: String): Regex
Transforms a String
value into a Regex
value.
Parameters
Name | Description |
---|---|
str |
The |
Example
This example shows how toRegex
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
toRegexEx1: toRegex("a-Z"),
toRegexEx2: toRegex("0-9+")
}
Output
1
2
3
4
{
toRegexEx1: /a-Z/,
toRegexEx2: /0-9+/
}
toString
toString(number: Number, format: String | Null = null, locale: String | Null = null, roundMode: RoundingMode | Null = null): String
A variant of toString
that transforms a Number
value
(whole or decimal) into a String
value and accepts a
format, locale, and rounding mode value.
Parameters
Name | Description |
---|---|
number |
The |
format |
The formatting to apply to the |
locale |
Optional ISO 3166 country code to use, such as |
roundMode |
Optional parameter for rounding decimal values
when the formatting presents a rounding choice,
such as a format of
|
Example
This example shows how toString
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * 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"),
f: toString(1.1234,"\$.## 'in my account'")
}
Output
1
2
3
4
5
6
7
8
{
"a": "1",
"b": ".01",
"c": "0,04",
"d": "0",
"e": ".04",
"f": "$1.12 in my account"
}
toString(date: Date | DateTime | LocalDateTime | LocalTime | Time, format: String | Null = null, locale: String | Null = null): String
A variant of toString
that transforms a Date
, DateTime
,
LocalTime
, LocalDateTime
, or Time
value into a String
value.
Parameters
Name | Description |
---|---|
date |
The |
format |
The ISO-8601 formatting to use on the date or time.
For example, this parameter accepts character patterns
patterns based on the Java 8
|
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toString
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
aDate: toString(|2003-10-01|, "uuuu/MM/dd"),
aDateTime: toString(|2018-09-17T22:13:00-03:00|),
aLocalTime: toString(|23:57:59|, "HH-mm-ss"),
aLocalDateTime : toString(|2015-10-01T23:57:59|),
aLocalDateTimeFormatted: toString(|2003-10-01T23:57:59|, "uuuu-MM-dd HH:mm:ss a"),
aLocalDateTimeFormattedAndLocalizedSpain: toString(|2003-01-01T23:57:59|, "eeee, dd MMMM, uuuu HH:mm:ss a", "ES"),
aTime: typeOf(|22:10:18Z|),
aTimeZone: toString(|-03:00|)
}
Output
1
2
3
4
5
6
7
8
9
10
{
"aDate": "2003/10/01",
"aDateTime": "2018-09-17T22:13:00-03:00",
"aLocalTime": "23-57-59",
"aLocalDateTime": "2015-10-01T23:57:59",
"aLocalDateTimeFormatted": "2003-10-01 23:57:59 PM",
"aLocalDateTimeFormattedAndLocalizedSpain": "miƩrcoles, 01 enero, 2003 23:57:59 p. m.",
"aTime": "Time",
"aTimeZone": "-03:00"
}
toString(binary: Binary, encoding: String): String
A variant of toString
that transforms a Binary
value
into a String
value with the specified encoding.
Parameters
Name | Description |
---|---|
binary |
The |
encoding |
The encoding to apply to the |
Example
This example shows how toString
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
var binaryData= "DW Test" as Binary {encoding: "UTF-32"}
output application/json
---
{
a: toString(binaryData, "UTF-32"),
}
Output
1
2
3
{
"a": "DW Test"
}
toString(data: TimeZone | Uri | Boolean | Period | Regex | Key): String
A variant of toString
that transforms a TimeZone
, Uri
,
Boolean
, Period
, Regex
, or Key
value into a
string.
Parameters
Name | Description |
---|---|
data |
The |
Example
This example shows how toString
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
transformTimeZone: toString(|Z|),
transformBoolean: toString(true),
transformPeriod: toString(|P1D|),
transformRegex: toString(/a-Z/),
transformPeriod: toString(|PT8M10S|),
transformUri: toString("https://docs.mulesoft.com/" as Uri)
} ++
{ transformKey : toString((keysOf({ "aKeyToString" : "aValue"})[0])) }
Output
1
2
3
4
5
6
7
8
9
{
"transformTimeZone": "Z",
"transformBoolean": "true",
"transformPeriod": "P1D",
"transformRegex": "a-Z",
"transformPeriod": "PT8M10S",
"transformUri": "https://docs.mulesoft.com/",
"transformKey": "aKeyToString"
}
toString(arr: Array<String>): String
A variant of toString
that joins an Array
of characters
into a single String
value.
Parameters
Name | Description |
---|---|
arr |
The |
Example
This example shows how toString
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
a: toString([]),
b: toString(["h", "o", "l", "a"])
}
Output
1
2
3
4
{
"a": "",
"b": "hola"
}
toTime
toTime(str: String, format: String | Null = null, locale: String | Null = null): Time
Transforms a String
value into a Time
value
and accepts a format and locale.
Parameters
Name | Description |
---|---|
str |
The |
format |
The formatting to use on the |
locale |
Optional ISO 3166 country code to use, such as |
Example
This example shows how toTime
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
a: toTime("23:57:59Z"),
b: toTime("13:44:12.283-08:00","HH:mm:ss.nxxx")
}
Output
1
2
3
4
{
a: |23:57:59Z|,
b: |13:44:12.000000283-08:00| as Time {format: "HH:mm:ss.nxxx"}
}
toTimeZone
toTimeZone(str: String): TimeZone
Transform a String
value into a TimeZone
value.
Parameters
Name | Description |
---|---|
str |
The |
Example
This example shows how toTimeZone
behaves with different inputs.
It produces output in the application/dw
format.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
toTimeZoneOffset: toTimeZone("-03:00"),
toTimeZoneAbbreviation: toTimeZone("Z"),
toTimeZoneName: toTimeZone("America/Argentina/Buenos_Aires")
}
Output
1
2
3
4
5
{
toTimeZoneOffset: |-03:00|,
toTimeZoneAbbreviation: |Z|,
toTimeZoneName: |America/Argentina/Buenos_Aires|
}
toUri
toUri(str: String): Uri
Transforms a String
value into a Uri
value.
Parameters
Name | Description |
---|---|
str |
The |
Example
This example shows how toUri
behaves.
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
toUriExample: toUri("https://www.google.com/")
}
Output
1
2
3
{
"toUriExample": "https://www.google.com/"
}
Types
MillisOrSecs
Type used for setting units to "milliseconds"
or "seconds"
.
1
"milliseconds" | "seconds"
PeriodUnits
Type used for setting units of a Period
value to "hours"
, "minutes"
, "seconds"
,
"milliseconds"
, or "nanos"
.
1
"hours" | "minutes" | "seconds" | "milliseconds" | "nanos"
RoundingMode
Type used when rounding decimal values up or down.
1
"UP" | "DOWN" | "CEILING" | "FLOOR" | "HALF_UP" | "HALF_DOWN" | "HALF_EVEN"