This post was originally published on DZone.

DataWeave is a new feature of Mule3 that allows us to convert data to any kind of format, such as XML, CSV, JSON and POJO’s, etc. In Mule 3, we use both MEL and Dataweave for writing the mule messages. Among these, MEL is the default expression language in Mule 3 But this approach had some data inconsistencies and scattered approaches. To avoid the stress of converting data objects to Java objects in Mule 3 every time by the usage of expressions Mule 4 was launched. In Mule 4 DataWeave is the default expression language over Mule 3’s default MEL.

In Mule 4 DataWeave version has changed from 1.0 to 2.0.

Apart from syntax changes, there are many new features in DataWeave 2.0.

  1. Language simplifications. Everything is now a function.
  2. DataWeave scripts can now be packaged and reused, via the new imports and modules features.
  3. Support for multi-line comments.
  4. Support for calling static Java functions directly from DataWeave.

DataWeave files are categorized into two main sections:

  1. Header – Which defines directives
  2. Body – Which describes the output structure

These two are delimited by a separator “—“.
The header contains directives, where we can define the following:

  • XTIVIA Blog CTA MuleSoftDataWeave version
  • Input types & sources
  • Output type
  • Namespaces to import
  • Constants
  • Functions etc.

In the body section, we can write the required transformation logic that defines the output structure.

1. Header Changes:

DataWeave version has changed from 1.0 to 2.0. Below are the changes to the header section of DataWeave:

  • dw version syntax changed from %dw 1.0 to %dw 2.0
  • Except %dw no longer is needed to start the directive with % in 2.0.
  • Functions in DataWeave 1.0 are defined with function directives. In DataWeave 2.0, it has been shortened to “fun”. Along with this, the function body and function signature must separate by = instead of a space.

Below, an example gives the same result in both Mule 3 & Mule 4.

Listing:1.A – DataWeave 1.0 Headers

%dw 1.0
%output application/json
%var user = {"firstName": "Murali", "lastName" : "TMR"}
%var a = 2
%input payload application/json
%function FName(data) (data.firstName)
%var Fullname = (data) -> (data.firstName ++ ' ' ++ data.lastName)
---
{
"A-Value" : a,
"FirstName" : FName(user),
"FullName" : Fullname(user)
}

Listing:1.B — DataWeave 2.0 Headers


%dw 2.0
output application/json
input payload application/json
var user = {"firstName": "Murali", "lastName" : "TMR"}
var a = 2
fun FName(data) = (data.firstName)
var Fullname = (data) -> (data.firstName ++ ' ' ++ data.lastName)
---
{
"A-Value" : a,
"FirstName" : FName(user),
"Fullname" : Fullname(user)
}

Listing:1.C — Output of Both Scripts


{
  "A-Value": 2,
  "FirstName": "Murali",
  "FullName": "Murali TMR"
}

2. Body Changes:

DataWeave body is the place to write transformation logic that defines output structure. Let’s look below for changes.

  1. Conditional Logic
  2. Operators are functions
  3. Type Names
  4. Comments

2.1 Conditional Logic:

DataWeave 1.0 uses when, unless, and otherwise to implement conditional logic. In DataWeave 2.0 these are replaced by if else, and else if.

Listing:2.1.A — DataWeave 1.0 Conditional Logic

%dw 1.0
%output application/json
%var data = ["Mon","Wed","Fri","Sat"]
---
{
Monday   : "true" when data contains "Mon" otherwise "false",
Tuesday  : "true" when data contains "Tue" otherwise "false",
Wednesday: "false" unless data contains "Wed" otherwise "true",
Thursday : "false" unless data contains "Thur" otherwise "true",
Friday   : "true" when data contains "fri"
otherwise ("trueSat" when data contains "Sat" 
otherwise "false")
}

Listing:2.1.B — DataWeave 2.0 Conditional Logic

%dw 2.0
output application/json
var data = ["Mon","Wed","Fri","Sat"]
---
{
Monday  : if ( data contains "Mon" ) "true" else "false",
Tuesday : if ( data contains "Tue" ) "true" else "false",
Wednesday : if (data contains "Wed") "true" else "false",
Thursday : if (data contains "Thu") "true" else "false",
Friday : if (data contains "fri") "true" 
else if (data contains "Sat") "trueSat"
else "false"
}

Listing: 2.1.C — Output of Both Scripts


{
  "Monday": "true",
  "Tuesday": "false",
  "Wednesday": "true",
  "Thursday": "false",
  "Friday": "trueSat"
}

2.2 Operators are Functions:

In DataWeave 2.0, all the operators are made as functions. This means these are surrounded by parenthesis.

Listing: 2.2.A — DataWeave 1.0 Operators

%dw 1.0
%output application/json
---
{
Addition: sum [1,2,3],
Size: sizeOf [1,2,3],
Type: typeOf "Murali",
Upper: upper "murali-tmr",
Plural-Form: pluralize "box",
Order : ordinalize 2
}

Listing:2.2.B — DataWeave 2.0 Operators

%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Addition : sum ([1,2,3]),
Size : sizeOf ([1,2,3]),
Type : typeOf("Murali"),
Upper : upper("mulesoft"),
PluralForm : pluralize("box"),
 Order : ordinalize(2)
}

Listing:2.2.C — Output of Both Scripts


{
  "Addition": 6,
  "Size": 3,
  "Type": ":string",
  "Upper": "MURALI-TMR",
  "Plural-Form": "boxes",
  "Order": "2nd"
}

2.3 Type Names:

In DataWeave 2.0 “:” was removed from the type names syntax. It looks as below.

Listing: 2.3.A — DataWeave 1.0 Types

%dw 1.0
%output application/json
%var pi = 3.14
---
{
Pi_Value: pi as :string,
Number: 123 as :string
}

Listing: 2.3.B — DataWeave 2.0 Types


%dw 2.0
output application/json
var pi = 3.14
---
{
Pi_Value: pi as String,
Number: 123 as String
}

Listing:2.3.C — Output of Both Scripts

{
  "Pi_Value": "3.14",
  "Number": "123"
}

2.4 Comments:

In DataWeave 1.0 we can give only single-line comments, but multi-line comments were introduced in DataWeave 2.0.

Listing: 2.4.A — DataWeave 1.0 comments


%dw 1.0
%output application/json
---
{
//Single-line comment
/* Multi-line comment not valid in DataWeave 1.0 */
}

Listing: 2.4.B — DataWeave 2.0 comments

%dw 2.0
output application/json
---
{
//Single-line comment
/* Multi-line comment was introduced from DataWeave 2.0 */
}

3. Conclusion:

As discussed throughout this MuleSoft blog, there are some changes when going from DataWeave 1.0 to DataWeave 2.0. Are you still having trouble navigating DataWeave 2.0 and MuleSoft? Our MuleSoft Anypoint experts can help get you past any issues you are currently experiencing, complete the project for you or help with anything in between. Contact us to speak with a MuleSoft expert now!

4. References:

Mule 4 and DataWeave 2.0

DataWeave 2.0 Functions

Share This