DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

DataWeave 1.0 to DataWeave 2.0 Migration – Part 1

Let's take a look at DataWeave 2.0 in comparison to 1.0.

Murali Thuraka user avatar by
Murali Thuraka
·
Jul. 06, 18 · Analysis
Like (5)
Save
Tweet
Share
36.25K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave is a new feature of Mule 3 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 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 defined directives
  2. Body ----> Which describes output structure

These two are delimited by a separator “---“.

The header contains directives, where we can define the following:

  • DataWeave version
  • Input types & sources
  • Output type
  • Namespaces to import
  • Constants
  • Functions etc.

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

1. Header Changes:

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

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

Below, an example gives the same result in both Mule 3 and 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 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 comment, but the multi-line comment was introduced from DataWeave 2.0 onwards.

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:

Above portion basically shuts this considerable measure of the broad post, so I don't figure I should state much here. Still here is something - Keep refreshing new DataWeave and Mule 4 to welcome the ride! Do disclose to me your musings or request related to this!

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building Restful APIs
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • How To Check Docker Images for Vulnerabilities
  • Agile Scrum and the New Way of Work in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: