%dw 1.0
Migrating from DataWeave version 1 to 2
DataWeave Header Content
Most of the header directives have been changed in DataWeave. The %dw
directive is an exception. In most cases, MuleSoft removed the %
, and in some cases the keyword is shortened.
DataWeave 1.0 | DataWeave 2.0 | Comments |
---|---|---|
|
||
|
|
In a Mule app flow, DataWeave 2.0 recognizes the input MIME type and its reader properties based on the MIME type ( |
|
|
|
|
|
|
|
|
|
|
|
Flow Controls
Flow control changed somewhat in DataWeave 2.
When Otherwise
The when otherwise
statement is replaced by if else
, for example:
{
orderStatus: "complete" when flowVars.purchaseOrderStatus == "C" otherwise "incomplete"
}
{
orderStatus: if(vars.purchaseOrderStatus == "C") "complete" else "incomplete"
}
Pattern Matcher
Pattern matching changed in DataWeave 2. It adds the keyword case
and else
(instead of default
). You also no longer separate cases with commas (,
) since they are now explicitly separated by the case
keyword.
'world' match {
:string -> true,
default -> false
}
'world' match {
case is String -> true
else -> false
}
For type pattern matchers, the is
keyword needs to be set.
Type References
The :
was removed from the type references and are now all camel case, so :string
is now String
Object Coercion
In DataWeave 1.0, selecting a key-value pair from an object required you to do something like this:
%var payload = {a: 1, b:2}
---
payload.a as :object
The DataWeave 1.0 expression above returns {a:1}
. Because this is a coercion, it is also included in the auto-coercion mechanism and generates undesired or unexpected results.
In DataWeave 2.0, the coercion is removed, and a new selector (&
) is introduced to select key-value pair parenthesis.
var payload = {a: 1, b:2}
---
payload.&a
This expression also returns {a:1}
.