Reading Time: 9 minutes

This post was written by one of the stars in our developer community, Manik Magar 

In my last post, I discussed DataWeave 2.0 header and body (iterative functions) changes in Mule 4 Beta. In this post, I will dive deeper into the topic by discussing operator changes (within the body) as well as other changes.

DataWeave 2.0 Operator Changes

As a language, DataWeave 1.0 defines many operators like isuppertypeOf, etc. which help in transformations. In DataWeave 2.0, there are several changes to the way operators are used in DataWeave 2.0.

1.1 Operators are now Functions

In DataWeave 2.0, all of these operators are now made as functions. What that means is they should be called like any other function calls, and with their arguments using parenthesis.

Consider the below DataWeave 1.0 and 2.0 code listings, which show the syntax for operators/functions and produce the same results.

Listing 2.2.1.A DataWeave 1.0 Operators
%dw 1.0
%output application/json
---
{
aType: typeOf "Manik",
aSize: sizeOf [1,2,3,4],
aUpper: upper "Manik"
}

Listing 2.2.1.B DataWeave 2.0 Operators
%dw 2.0
output application/json
---
{
aType: typeOf("Manik"),
aSize: sizeOf([1,2,3,4]),
aUpper: upper("Manik")
}

1.2 Range Selection

DataWeave 1.0 uses .. operator to select ranges. This has been replaced with a new to operator.

Consider the DataWeave 1.0 and 2.0 code listings below, which show the syntax for operators/functions and produce the same results.

Listing 2.2.2.A DataWeave 1.0 Range Selector
%dw 1.0
%output application/json
%var data = [1,2,3,4,5,6,7,8,9]
%var m = 3
%var n = 5
---
{
subArray: data[2..4],
//Throws error
subArray2: data [m..n],
subString: "ABCDEFG" [1..3]
}

Using dynamic ranges with .. was very error-prone and not straightforward. This throws an error in DataWeave 1.0.

Listing 2.2.2.B DataWeave 2.0 Range Selector
%dw 2.0
output application/json
var data = [0,1,2,3,4,5,6,7,8,9]
var m = 3
var n = 5
---
{
subArray: data [2 to 4],
subArray2: data [m to n],
subString: "ABCDEFG" [1 to 3]

}

Using dynamic variables to define range is straightforward and easy! .. is still a valid descendants selector for DataWeave 2.0.

1.3 Conditional Logic

DataWeave 1.0 uses whenunless (probably rarely used) and otherwise in order to implement conditional logic. All of these keywords have been replaced by if and else in DataWeave 2.0.

Consider the DataWeave 1.0 and 2.0 code listings below, which show the syntax for operators/functions and produce the same results.

Listing 2.2.3.A DataWeave 1.0 Conditional Logic
%dw 1.0
%output application/json
%var data = [1,2,3,4,5,6,7,8,9]
---
{
has2: "true" when data contains 2 otherwise "false",
has22: "true" when data contains 22 otherwise "false",
has21: "false" unless data contains 21 otherwise "true",
has2: "false" unless data contains 2 otherwise "true",
has22Or5: "true" when data contains 22
otherwise ("true5"
when data contains 5 otherwise "false"
)
}

Listing 2.2.3.B DataWeave 2.0 Conditional Logic
%dw 2.0
output application/json
var data = [0,1,2,3,4,5,6,7,8,9]
---
{
has2: if (data contains 2) "true" else "false",
has22: if (data contains 22) "true" else "false",

//outputs true5
has22or5: if (data contains 22) "true"
else if (data contains 5) "true5"
else "false"
}

Nested conditionsif .. else if .. else feels so familiar that of in Java!

1.4 Binary functions

Binary functions are the functions need two arguments. In DataWeave 1.0, they have a notation of argument1 function argument2. In DataWeave 2.0, there is now an added alternate notation to all of these binary functions, making them really look like function calls.

Consider the DataWeave 2.0 script below, where all key1 like calls are valid for both DataWeave versions and key2 like calls are new notations added in DataWeave 2.0.

Listing 2.2.4.A DataWeave Binary Functions
%dw 2.0
output application/json
var data = [0,1,2,3,4,5,6,7,8,9]
---
{
contains1: data contains 2,
contains2: contains(data, 2),
starts1: "Manik" startsWith "M",
starts2: startsWith("Manik", "N")
}

Other DataWeave Syntax Changes

Besides the changes outlined above and in my last post, there are also a few other syntax changes that have been made to DataWeave 2.0. Here are some of those changes:

  • Removed : from all type names. They are now written with first upper case letter i.e Object instead of :object
  • Added a new key-value pair selector .& that returns all matching key-values as a single object. For example, {"firstName": "MM1", "addr": "addr1", "addr": "addr2"}.&addr will return an object {"addr": "addr1","addr": "addr2"}
  • Namespace prefixes can no longer contain the character -
  • New namespace selector (.#) returns the namespace used.
  • New supported syntax for match when using arrays [head ~ tail]

The power of DataWeave

I looked at the syntax changes in DataWeave 2.0 compared them to DataWeave 1.0 and if you are thinking that the changes covered in my two posts are the only changes, then you are missing lot of new features!

As I mentioned earlier, DataWeave 2.0 is the default expression language in Mule 4 Beta. This opens multiple opportunities to leverage DataWeave’s power throughout your Mule Flow.

Apart from that, Mule 4 Beta also introduced concepts like reusable modules, which can contain your common functions, operators, etc. Many new functions and operators were also added; this includes features like importing Java classes and calling static methods from within DataWeave, which enrich the overall DataWeave experience.

Finally, I strongly, suggest you go through the DataWeave documentation (reference links are at the bottom) and make yourself familiar with DataWeave 2.0!

Keep learning more about Mule 4 Beta and enjoy the ride, and let me know if you have any thoughts or questions in the comments!

Interested in learning more?

This article first appeared on JavaStreets.


latest report
Learn why we are the Leaders in API management and iPaaS