Contact Us 1-800-596-4880

Migrating from DataWeave version 1 to 2

Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule reached its End of Life on November 2, 2022, 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.

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

%dw 1.0

%dw 2.0

%output

output

%var

var

%function

fun

%namespace

ns

Flow Controls

Flow control changed somewhat in DataWeave 2.

When Otherwise

The when otherwise statement is replaced by if else, for example:

Mule 3 Example: DataWeave 1
{
    orderStatus: "complete" when flowVars.purchaseOrderStatus == "C" otherwise "incomplete"
}
Mule 4 Example: DataWeave 2
{
    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.

Mule 3 Example: DataWeave 1
'world' match {
    :string -> true,
    default -> false
  }
Mule 4 Example: DataWeave 2
'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:

Mule 3 Example: DataWeave 1
%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.

Mule 3 Example: DataWeave 2
var payload = {a: 1, b:2}
---
payload.&a

This expression also returns {a:1}.

Conditional Key-Value Parenthesis

In DataWeave 1.0, conditional key-value pairs are declared with the when keyword.

Mule 3 Example: DataWeave 1
{
    (a: 1) when payload.product == "Mule"
}

In DataWeave 2.0, you use the if keyword.

Mule 4 Example: DataWeave 2
{
    (a: 1) if payload.product == "Mule"
}