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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • DZone Community Awards 2022
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Beginners Guide for Web Scraping Using Selenium
  • Projection Queries: A Way to Optimize Data Traffic

Trending

  • Alternative Structured Concurrency
  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Mocking Kafka for Local Spring Development
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  1. DZone
  2. Data Engineering
  3. Data
  4. DataWeave 1.0 to DataWeave 2.0 Migration – Part 1

DataWeave 1.0 to DataWeave 2.0 Migration – Part 1

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

By 
Murali Thuraka user avatar
Murali Thuraka
·
Jul. 06, 18 · Analysis
Likes (5)
Comment
Save
Tweet
Share
38.5K 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!

Listing (computer) Operator (extension) Data (computing) Syntax (programming languages) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • DZone Community Awards 2022
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Beginners Guide for Web Scraping Using Selenium
  • Projection Queries: A Way to Optimize Data Traffic

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook