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

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"


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!


  1. bitlevex is legit

    Hey! I understand this is sort of off-topic but I needed to ask. Does building a well-established blog such as yours take a large amount of work? I am brand new to blogging but I do write in my journal daily. I’d like to start a blog so I will be able to share my experience and views online. Please let me know if you have any kind of recommendations or tips for brand new aspiring bloggers. Thankyou!|

  2. brisbane seo

    hello there and thank you for your information – I have definitely picked up something new from right here. I did however expertise some technical issues using this web site, as I experienced to reload the site a lot of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and can look out for much more of your respective exciting content. Make sure you update this again soon..

  3. plumbing services important site

    There are actually a lot of details like that to take into consideration. That could be a nice level to convey up. I provide the ideas above as general inspiration but clearly there are questions just like the one you deliver up the place the most important factor shall be working in honest good faith. I don?t know if best practices have emerged around issues like that, but I am certain that your job is clearly identified as a fair game. Each boys and girls feel the impression of only a second’s pleasure, for the remainder of their lives.

  4. check over here 24 hr electrician

    I was curious if you ever thought of changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or 2 pictures. Maybe you could space it out better?

  5. Ariana Teppo

    I and also my pals have been reviewing the best guidelines on the blog then then came up with a horrible feeling I had not thanked the site owner for them. All the young boys appeared to be consequently thrilled to study them and now have in actuality been enjoying them. I appreciate you for actually being indeed accommodating as well as for getting some awesome information millions of individuals are really eager to discover. Our own sincere apologies for not expressing appreciation to you earlier.

  6. air conditioner repair

    Hiya, I am really glad I’ve found this info. Nowadays bloggers publish only about gossips and web and this is really annoying. A good web site with interesting content, that is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Cant find it.

  7. Cora Center

    Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is excellent blog. An excellent read. I will definitely be back.

  8. Chang Gile

    Aw, this was a really nice post. In idea I wish to put in writing like this additionally – taking time and precise effort to make an excellent article… but what can I say… I procrastinate alot and in no way appear to get one thing done.

  9. Morton Hinson

    That is really attention-grabbing, You are a very skilled blogger. I’ve joined your rss feed and look forward to in search of extra of your magnificent post. Also, I’ve shared your website in my social networks!

  10. Lincoln Beyale

    It’s a pity you don’t have a donate button! I’d certainly donate to this fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this website with my Facebook group. Chat soon!

  11. Renate

    Howdy! Would you mind if I share your blog with my twitter group?

    There’s a lot of people that I think would really appreciate your content.

    Please let me know. Thank you

  12. Francisco

    Undeniably believe that which you said. Your favorite justification seemed to
    be on the internet the simplest factor to understand of.

    I say to you, I definitely get irked at the same time as people consider issues that they plainly don’t understand about.
    You controlled to hit the nail upon the highest and defined out the whole thing without having side-effects , other folks can take a
    signal. Will likely be back to get more. Thank you

  13. Luella

    Hello there! I know this is kinda off topic however I’d
    figured I’d ask. Would you be interested in exchanging
    links or maybe guest writing a blog article or vice-versa?
    My website goes over a lot of the same subjects as yours and I feel we could greatly benefit
    from each other. If you might be interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Great blog by the way!

  14. Gary

    It’s amazing to visit this web page and reading the views of all mates regarding this post, while I am also
    eager of getting familiarity.

  15. 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

  16. 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!

  17. 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.

  18. 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.

  19. 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!

  20. 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!

  21. 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.

  22. 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.