<items>
<item>
<price>22.30</price>
</item>
<item>
<price>20.31</price>
</item>
</items>
Type Coercion with DataWeave
In DataWeave, types can be coerced from one type to other using the as
operator. Type coercion takes place at runtime.
Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For
DataWeave in Mule 3 apps, refer to the
DataWeave version 1.2 documentation.
For other Mule versions, you can use the version selector in the DataWeave table of contents.
Note that when you provide an operator with properties that do not match the expected types, DataWeave automatically attempts to coerce the provided property to the required type.
Defining DataWeave Types For Type Coercion
The DataWeave example defines the type Currency
using the String
type, formats the value with the Java DecimalFormat pattern (##
), and then uses as
to coerce the price
values to the Currency
type.
%dw 2.0
output application/json
type Currency = String { format: "\$#,###.00"}
---
books: payload.items.*item map
book:
price: $.price as Currency
{
"books": [
{
"book": {
"price": "22.30"
}
},
{
"book": {
"price": "20.31"
}
}
]
}
Type Coercion Table
This table shows the possible combinations and the properties from the schema that are used in the transformation.
Source | Target | Property |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns a string with all the values of the range using |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Properties for Type Coercion
Property | Description |
---|---|
|
Accepts Java classes for Object types. |
|
Accepts Java |
|
Accepts Java locales. A Java |
|
Parsing mode for date and time values. Valid values: Note that |
|
Value can be |
The following examples show uses of the mode
property. Notice that LENIENT
and SMART
return valid dates when they receive an invalid date such as 02/31/2020
, but the resulting dates differ. STRICT
returns an error on an invalid date. The examples use uuuu
to represent the year instead of yyyy
.
%dw 2.0
output application/json
---
{
examples : {
badDateWithLenient: '02/31/2020' as Date {mode: "LENIENT", format: 'MM/dd/uuuu'},
badDateWithSmart: '02/31/2020' as Date {mode: "SMART", format: 'MM/dd/uuuu'}
}
}
%dw 2.0
output application/json
---
{
"examples": {
"badDateWithLenient": "03/02/2020",
"badDateWithSmart": "02/29/2020"
}
}
Using STRICT on an invalid date returns an error. The following example is an error returned by badDateWithStrict: '02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}
:
"Cannot coerce String (02/31/2020) to Date, caused by: Text '02/31/2020' could not be parsed: Invalid date 'FEBRUARY 31'
9| badDateWithSmart:
'02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The following example converts two strings into a date format and concatenates the result. The first string represents the date, and the second string represents the time. The transformation uses as
to coerce the first string to LocalDateTime
, and then to a String
with the specified format. The transformation also uses ++
to concatenate the result.
%dw 2.0
output application/json
var s1= 20201228 // (uuuuMMdd),
var s2= 1608 //(HHMM)
---
(s1 ++ s2)
as LocalDateTime {format:"uuuuMMddHHmm"}
as String {format:"MM-dd-uuuu HH:mm:ss"}
"12-28-2020 16:08:00"
The following example also converts a string into a date format. First, the transformation uses as
to coerce the string into a LocalDateTime
type, and then it coerces the result into a String
type with the specified output format.
%dw 2.0
output application/json
---
"8/30/2020 4:00:13 PM"
as LocalDateTime {format: "M/dd/uuuu h:mm:ss a"}
as String {format: "MM/dd/uuuu"}
"08/30/2020"
This example converts a number to a string with a decimal representation:
%dw 2.0
output application/json
---
{
data: 22 as String {format: ".00"}
}
{
"data": "22.00"
}
To maintain consistency across all formats, starting with DataWeave 2.5, writing number values directly doesn’t preserve formatting and strips trailing zeroes. Format metadata is only used to parse the provided string into the correct number representation. To retain this behavior, you can re-enable it by using the stripTrailingZeroes property. See DataWeave System Properties.
|
The following example uses the locale
property to format number and date values. First, you coerce a number value into String
type to the specified output format that uses the Java DecimalFormat pattern (##) and also the locale
property en
(English) or es
(Spanish). The locale: "en"
property, formats the output number decimal representation using a .
, while the locale: "es"
property, formats the output using a ,
.
Then, you coerce a date value into Date
type and then to String
type with the output format dd-MMM-yy
and the locale
property en
(English), or es
(Spanish). The locale: "en"
property, formats the month MMM
in English, while the locale: "es"
property formats the month in Spanish.
%dw 2.0
output application/json
---
{
enNumber: 12.3 as String {format: "##.##", locale: "en"},
esNumber: 12.3 as String {format: "##.##", locale: "es"},
esDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "es"},
enDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "en"}
}
{
"enNumber": "12.3",
"esNumber": "12,3",
"esDate": "31-dic.-20",
"enDate": "31-Dec-20"
}