Contact Us 1-800-596-4880

Flow Control and Scope Operations in 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.

You can use the following operators within any DataWeave expression:

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.

do

A do statement creates a scope in which new variables, functions, annotations, or namespaces can be declared and used. The syntax is similar to a mapping in that it is composed of a header and body separated by ---. Its header is where all the declarations are defined, and its body is the result of the expression.

This example uses do to return the string "DataWeave" when myfun() is called from the main body of the script.

DataWeave Script:
%dw 2.0
output application/json
fun myfun() = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myfun() }

This example uses do to return the string "DataWeave" when the variable myVar is referenced from the main body of the script.

DataWeave Script:
%dw 2.0
output application/json
var myVar = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myVar }

Both scripts produce this output:

Output:
{
  "result": "DataWeave"
}

The next example uses do to prepend the string "Foo" to a string (" Bar") that is passed to the test(p: String) function.

DataWeave Script:
%dw 2.0
output application/json
fun test(p: String) = do {
    var a = "Foo" ++ p
    ---
    a
}
---
{ result: test(" Bar") }
Output:
{
  "result": "Foo Bar"
}

if else

An if statement evaluates a conditional expression and returns the value under the if only if the conditional expression is true. Otherwise, it will return the expression under else. Every if expression must have a matching else expression. The following example uses the input { country : "FRANCE" }, which is defined by the myVar variable in the header:

DataWeave Script:
%dw 2.0
var myVar = { country : "FRANCE" }
output application/json
---
if (myVar.country == "USA")
  { currency: "USD" }
else { currency: "EUR" }
Output
{
  "currency": "EUR"
}

else if

You can chain several else expressions together within an if-else construct by incorporating else if. The following example uses the input var myVar = { country : "UK" }, which is defined by the myVar variable in the header.

DataWeave Script:
%dw 2.0
var myVar = { country : "UK" }
output application/json
---
if (myVar.country =="USA")
	{ currency: "USD" }
else if (myVar.country =="UK")
	{ currency: "GBP" }
else { currency: "EUR" }
Output
{
  "currency": "GBP"
}