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, formatters: Array<Formatter>): Date
Transforms a String
value into a Date
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
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
9
%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
a: toDate("2023-28-03", [{format: "yyyy/MM/dd"}, {format: "yyyy-dd-MM", locale: "en_US"}]),
b: try(() -> toDate("2023-28-03", [{format: "yyyy/MM/dd"}])).error.message
}
Output
1
2
3
4
{
a: |2023-03-28| as Date {format: "yyyy-dd-MM", locale: "en_US"},
b: "Could not find a valid formatter for '2023-28-03'"
}
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"}
}
toDateOrNull
toDateOrNull(str: String, formatters: Array<Formatter>): Date | Null
Transforms a String
value into a Date
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toDateOrNull
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: toDateOrNull("2023-28-03", [{format: "yyyy/MM/dd"}, {format: "yyyy-dd-MM", locale: "en_US"}]),
b: toDateOrNull("2023-28-03", [{format: "yyyy/MM/dd"}])
}
Output
1
2
3
4
{
a: |2023-03-28| as Date {format: "yyyy-dd-MM", locale: "en_US"},
b: null
}
toDateTime
toDateTime(str: String, formatters: Array<Formatter>): DateTime
Transforms a String
value into a DateTime
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
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
9
%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
a: toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}, {format: "uuuu-MM-dd HH:mm:ssz"}]),
b: try(() -> toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}])).error.message
}
Output
1
2
3
4
{
a: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"},
b: "Could not find a valid formatter for '2003-10-01 23:57:59Z'"
}
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"}
}
toDateTimeOrNull
toDateTimeOrNull(str: String, formatters: Array<Formatter>): DateTime | Null
Transforms a String
value into a DateTime
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toDateTimeOrNull
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: toDateTimeOrNull("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}, {format: "uuuu-MM-dd HH:mm:ssz"}]),
b: toDateTimeOrNull("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}])
}
Output
1
2
3
4
{
a: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"},
b: null
}
toLocalDateTime
toLocalDateTime(str: String, formatters: Array<Formatter>): LocalDateTime
Transforms a String
value into a LocalDateTime
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
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
9
%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
a: toLocalDateTime("2003-10-01 23:57:59", [{format: "uuuu/MM/dd HH:mm:ss"}, {format: "uuuu-MM-dd HH:mm:ss"}]),
b: try(() -> toLocalDateTime("2003-10-01 23:57:59", [{format: "uuuu/MM/dd HH:mm:ss"}])).error.message
}
Output
1
2
3
4
{
a: |2003-10-01T23:57:59| as LocalDateTime {format: "uuuu-MM-dd HH:mm:ss"},
b: "Could not find a valid formatter for '2003-10-01 23:57:59'"
}
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"}
}
toLocalDateTimeOrNull
toLocalDateTimeOrNull(str: String, formatters: Array<Formatter>): LocalDateTime | Null
Transforms a String
value into a LocalDateTime
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toLocalDateTimeOrNull
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: toLocalDateTimeOrNull("2003-10-01 23:57:59", [{format: "uuuu/MM/dd HH:mm:ss"}, {format: "uuuu-MM-dd HH:mm:ss"}]),
b: toLocalDateTimeOrNull("2003-10-01 23:57:59", [{format: "uuuu/MM/dd HH:mm:ss"}])
}
Output
1
2
3
4
{
a: |2003-10-01T23:57:59| as LocalDateTime {format: "uuuu-MM-dd HH:mm:ss"},
b: null
}
toLocalTime
toLocalTime(str: String, formatters: Array<Formatter>): LocalTime
Transforms a String
value into a LocalTime
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toLocalTime
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
import * from dw::Runtime
output application/dw
---
{
a: toLocalTime("23:57:59", [{format: "HH:mm:ss.n"}, {format: "HH:mm:ss"}]),
b: try(() -> toLocalTime("23:57:59", [{format: "HH:mm:ss.n"}])).error.message
}
Output
1
2
3
4
{
a: |23:57:59| as LocalTime {format: "HH:mm:ss"},
b: "Could not find a valid formatter for '23:57:59'"
}
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"
}
toLocalTimeOrNull
toLocalTimeOrNull(str: String, formatters: Array<Formatter>): LocalTime | Null
Transforms a String
value into a LocalTime
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toLocalTimeOrNull
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: toLocalTimeOrNull("23:57:59", [{format: "HH:mm:ss.n"}, {format: "HH:mm:ss"}]),
b: toLocalTimeOrNull("23:57:59", [{format: "HH:mm:ss.n"}])
}
Output
1
2
3
4
{
a: |23:57:59| as LocalTime {format: "HH:mm:ss"},
b: null
}
toNumber
toNumber(str: String, formatters: Array<Formatter>): Number
Transforms a String
value into a Number
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<DatesFormatter> |
The |
Example
This example shows how toNumber
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
import * from dw::Runtime
output application/dw
---
{
a: toNumber("0.005", [{format: "seconds"}, {format: ".00"}]),
b: try(() -> toNumber("0.005", [{format: "seconds"}])).error.message
}
Output
1
2
3
4
{
a: 0.005 as Number {format: ".00"},
b: "Could not find a valid formatter for '0.005'"
}
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
}
toNumberOrNull
toNumberOrNull(str: String, formatters: Array<Formatter>): Number | Null
Transforms a String
value into a Number
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toNumberOrNull
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: toNumberOrNull("0.005", [{format: "seconds"}, {format: ".00"}]),
b: toNumberOrNull("0.005", [{format: "seconds"}])
}
Output
1
2
3
4
{
a: 0.005 as Number {format: ".00"},
b: null
}
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, formatters: Array<Formatter>): Time
Transforms a String
value into a Time
value using the first Formatter
that
matches with the given value to transform.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
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
9
%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
a: toTime("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}, {format: "HH:mm:ss.nxxx"}]),
b: try(() -> toTime("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}]).error.message
}
Output
1
2
3
4
{
a: |13:44:12.000000283-08:00| as Time {format: "HH:mm:ss.nxxx"},
b: "Could not find a valid formatter for '13:44:12.283-08:00'"
}
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"}
}
toTimeOrNull
toTimeOrNull(str: String, formatters: Array<Formatter>): Time | Null
Transforms a String
value into a Time
value using the first Formatter
that matches
with the given value to transform.
If none of the Formatter
matches with the given value returns a null
value.
Parameters
Name | Type | Description |
---|---|---|
|
String |
The |
|
Array<Formatter> |
The |
Example
This example shows how toTimeOrNull
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: toTimeOrNull("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}, {format: "HH:mm:ss.nxxx"}]),
b: toTimeOrNull("13:44:12.283-08:00", [{format: "HH:mm:ss.xxx"}])
}
Output
1
2
3
4
{
a: |13:44:12.000000283-08:00| as Time {format: "HH:mm:ss.nxxx"},
b: null
}
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
Formatter
Type used for formatting Dates
types and Number
.
Supports the following fields:
-
format
: (optional) The ISO-8601 formatting to use on the date or time. For example, this parameter accepts character patterns patterns based on the Java 8java.time.format
. Anull
value has no effect on the value. -
locale
: (optional) ISO 3166 country code to use, such asUS
,AR
, orES
. Anull
or absent value uses your JVM default. When you pass a translatable format, such aseeee
andMMMM
, alocale
(such as"ES
) transforms the corresponding numeric values to a localized string
1
{ format?: String, locale?: String }
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"