<items>
<item>
<price>22.30</price>
</item>
<item>
<price>20.31</price>
</item>
</items>
Type Coercion with DataWeave
DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended. 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.
Before you begin, note that DataWeave version 2 is for Mule 4 apps. For a Mule 3 app,
refer to the
DataWeave 1.0 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 |
|
Value can be |
Using a Java Bridge
DataWeave enables you to call any Java static function or constructor by using the Java bridge. This feature is useful when you need to reuse business logic written in Java or to instantiate any Java object that does not have an empty public constructor.
To use a Java bridge, transform a fully qualified Java name to a DataWeave name:
-
Add the prefix
!java
. -
Replace the dots in the fully qualified name with
::
.
When invoking the Java function, DataWeave transforms each argument to the expected type in the function parameter.
The following example shows how to create a new instance of a java.lang.NullPointerException
:
%dw 2.0
output application/json
---
java!java::lang::NullPointerException::new("foo")
The following example invokes the function java.lang.String.valueOf
:
%dw 2.0
output text/plain
import valueOf from java!java::lang::String
---
valueOf(true)