Mule 4 Mulesoft Basics Mulesoft Tutorial

DataWeave 1.0 to DataWeave 2.0 Migration – Part -1


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 & 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 at 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("murali-tmr"),
  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 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!


20 comments

  1. Kevin David

    Nice blog here! Also your website loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol

  2. Kevin David

    Pretty nice post. I just stumbled upon your weblog and wished to say that I’ve truly loved browsing your weblog posts. After all I will be subscribing for your rss feed and I am hoping you write once more very soon!

  3. qq pedia

    All those guidelines additionally worked to become good way to understand that other people on-line have the identical veneración like mine to understanding great deal more about this condition.

  4. kerajinan dari barang bekas

    Prevailing without the answers to be able to the difficulties you’ve categorized out through this manual is a critical situation, as well as the particular kind which could have got badly affected my whole career if I acquired not discovered your site.

  5. look for puppy

    An impressive share, I just given this onto a colleague who was doing a bit evaluation on this. And he in reality bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for spending the time to discuss this, I feel strongly about it and love studying extra on this topic. If possible, as you turn out to be experience, would you mind updating your weblog with extra details? It is extremely useful for me. Huge thumb up for this weblog submit!

  6. Jacinto Michioka

    Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

  7. Celena Woytek

    I’m preparing to start my blog quickly, but I’m a little lost on most things. Would you suggest beginning with a free platform like WordPress or opt for a paid alternative? There are numerous options around that I’m totally confused. Love to hear recommendations? Many thanks a lot.

  8. cheap seo services

    Successful search engine optimization can be accomplished by integrating a site map into your website. This enables search engines to find other pages in your site other than the homepage easily and, thus, enhances your presence on search engines. Be sure to link each page of your website to your site map.

Leave a Reply

Your email address will not be published.