Defining & Using Variables in DataWeave 2.0 (Mule 4)

Photo by Florian Olivo on Unsplash

DataWeave is the MuleSoft expression language for accessing and transforming data that travels through a Mule app. DataWeave is tightly integrated with the Mule runtime engine, which runs the scripts and expressions in your Mule app. DataWeave is a functional language in which variables behave just like functions. The general format of a DataWeave script contains 3 sections: Header, delimiter, and body as shown in the exhibit below.

Defining and using global variables

We can create a global variable in the header using var directive. We need to give it a name and we can assign either a constant value or a lambda expression. Global variables can be referenced by their name from anywhere in the body of the DataWeave script.

Let us see an example. In the exhibit below, we initialize 3 global variables.

The first one “mname” is assigned the literal string value “the”. The second one “mname2” is assigned a lambda expression which returns a literal string value “other”. The third one “lname” is assigned a lambda expression that takes a string as an input, passes it to the upper function, and returns the string in uppercase.

DataWeave also gives an alternate way to define a variable as a function in a syntax similar to traditional functions. It may be easier to read for some people. We have to use the fun directive in this way. The following exhibit shows the original syntax for defining lname as in the previous example and the alternative for the same using fun directive. The behavior is identical in both cases.

Defining and using local variables

Local variables are initialized inside the body of the DataWeave script using the keyword “using” with the syntax using(<variable-name> = <expression>) Local variables can only be referenced from within the scope of the expression where they are initialized. You can declare multiple local variables in a “do” scope header with the syntax do{<variable declaration header> --- <body>}

Let us see an example of creating a local variable with “do” scope

  • In the first example, a local variable called “name” is declared then used as a transformation expression.
  • The second example defines two local variables “fname” & “lname” in an outer scope and two local variables “user” & “color” in an inner scope. It shows that the inner scope can reference the variable in the outer scope by assigning “fname” to “user”.
  • The third example is similar to the second but shows that the outer scope cannot reference the variable defined in the inner scope when it tries to assign color value to a color property. This results in an error.

This is how you define and use global and local variables using DataWeave 2.0 in Mule 4.

Give claps if you found this article useful. Stay tuned for upcoming articles.

All the images/exhibits are from the author unless given credit

Thanks for reading the article! Wanna connect with me?
Here is a link to my Linkedin Profile

Keep it simple! stay happy!