<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 DataWeave version 2 is for Mule 4 apps. For a Mule 3 app,
refer to the
DataWeave version 1 documentation set
in the Mule 3.9 documentation. For other Mule versions, you can use
the version selector for the Mule Runtime 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"
The following example converts a string to a number with decimal representation. First, you transform the string value "22"
to Number
and then to String
again because format
adds zeros when converting from a number to a string. If the input data is the number 22
, it is not necessary to perform the first Number
conversion.
The latter Number
conversion makes the output a number with the decimal representation.
%dw 2.0
output application/json
---
{
data: "22" as Number as String {format: ".00"} as Number
}
{
"data": 22.00
}
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"
}