<items>
<item>
<price>22.30</price>
</item>
<item>
<price>20.31</price>
</item>
</items>
Type Coercion with DataWeave
DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
In DataWeave, types can be coerced from one type to other using the as
operator. Type coercion takes place at runtime.
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|