Mule 4 Mulesoft Basics Mulesoft Tutorial

Mule – 4 DataWeave Functions – Part – 1


In DataWeave 2.0 functions are categorized into different modules.

  1. Core (dw::Core)
  2. Arrays (dw::core::Arrays)
  3. Binaries (dw::core::Binaries)
  4. Encryption (dw::Crypto)
  5. Diff (dw::util::Diff)
  6. Objects (dw::core::Objects)
  7. Runtime (dw::Runtime)
  8. Strings (dw::core::Strings)
  9. System (dw::System)
  10. URL (dw::core::URL)

Functions defined in Core (dw::Core) module are imported automatically into your DataWeave scripts. To use other modules, we need to import them by adding the import directive to the head of DataWeave script, for example:

import dw::core::Strings

import dasherize, underscore from dw::core::Strings

import * from dw::core::Strings

Sample Payload:
{
"firstName" : "Murali",
"lastName" : "Krishna",
"age" : "26",
“age” : ”26”
}

1. Core (dw::Core)

Below are the DataWeave 2 core functions:

++ , –, abs, avg, ceil, contains, daysBetween, distinctBy, endsWith, filter, IsBlank, joinBy, min, max etc….

result : [0, 1, 2] ++ [“a”, “b”, “c”] will gives us “result” : “[0, 1, 2, “a”, “b”, “c”]”

result : [0, 1, 1, 2] — [1,2] will gives us “result” : “[0]”

result : abs(-20) will gives us “result” : 20

average : avg([1, 1000]) will gives us “average” : 500.5

value : ceil(1.5) will gives us “value” : 2

result : payload contains “Krish” will gives us “result” : true

days: daysBetween(“2016-10-01T23:57:59-03:00”, “2017-10-01T23:57:59-03:00”) will gives us “days”: 365

age : payload distinctBy $ will gives us  :

 {

“firstName” : “Murali”,

“lastName” : “Krishna”,

“age” : ”26”

}

a: “Murali” endsWith “li” will gives us “a” : true

a: [1, 2, 3, 4, 5] filter($ > 2) will gives us “a” : [3,4,5]

empty: isBlank(“”) will gives us “empty” : true

aa: [“a”,”b”,”c”] joinBy “-” will gives us “a” : “a-b-c”

a: min([1, 1000]) will gives us “a” : 1

a: max([1, 1000]) will gives us “a” : 1000

2.Arrays (dw::core::Arrays)

Arrays related functions in DataWeave are :

countBy, divideBy, every, some, sumBy

[1, 2, 3] countBy (($ mod 2) == 0) will gives us 1

[1,2,3,4,5] dw::core::Arrays::divideBy 2 will gives us :

[

[

1,

2

],

[

3,

4

],

[

5

]

]

 

[1,2,3,4] dw::core::Arrays::every ($ == 1) will gives us “false”

[1,2,3,4] dw::core::Arrays::some ($ == 1) will gives us “true”

[ { a: 1 }, { a: 2 }, { a: 3 } ] sumBy $.a will gives us “6”

3.Binaries (dw::core::Binaries)

Binary functions in DataWeave-2 are:

fromBase64, fromHex, toBase64, toHex

toBase64(fromBase64(12463730)) will gives us “12463730”

{ “binary”: fromHex(‘4D756C65’)} will gives us “binary” : “Mule”

{ “hex” : toHex(‘Mule’) } will gives us “hex” : “4D756C65”

4.Encryption (dw::Crypto)

Encryption functions in Dataweave – 2 are:

HMACBinary, HMACWith, MD5, SHA1, hashWith

{ “HMAC”: Crypto::HMACBinary((“aa” as Binary), (“aa” as Binary)) } will gives us :

“HMAC”: “\u0007£š±]\u00adÛ\u0006‰\u0006Ôsv:ý\u000b\u0016çÜð”

Crypto::MD5(“asd” as Binary) will gives us “7815696ecbf1c96e6894b779456d330e”

Crypto::SHA1(“dsasd” as Binary) will gives us “2fa183839c954e6366c206367c9be5864e4f4a65”

5.Diff (dw::util::Diff)

It calculates difference between two values and returns list of differences.

DataWeave Script:

%dw 2.0

import * from dw::util::Diff

output application/json

var a = { age: “Test” }

var b = { age: “Test2” }

a diff b

Output:

{

“matches”: false,

“diffs”: [

{

“expected”: “\”Test\””,

“actual”: “\”Test2\””,

“path”: “(root).age”

}

]

}

Note:

Rest of the things will proceed in Mule – 4 DataWeave Functions Part – 2 article


60 comments

  1. Cesar

    Hello there! This is my 1st comment here so I just wanted
    to give a quick shout out and tell you I truly enjoy reading through your blog posts.

    Can you recommend any other blogs/websites/forums that go over the same topics?
    Thanks a ton!

  2. dasdas

    Hello there, just became aware of your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I?ll be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  3. Mckenzie

    We are a group of volunteers and starting a new scheme in our
    community. Your web site offered us with valuable info to work on. You’ve done an impressive job and our whole community will be thankful to you.

  4. Kristan

    Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed account your blog posts.

    Any way I will be subscribing to your augment and even I
    achievement you access consistently rapidly.

  5. Mollie

    Heya i’m for the first time here. I came across this board and I find It really helpful & it
    helped me out a lot. I am hoping to give one thing again and aid others such
    as you helped me.

  6. Susie

    Hey there this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors
    or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from
    someone with experience. Any help would be greatly appreciated!

  7. Johnette

    You can definitely see your skills in the work you write.
    The world hopes for more passionate writers like you who are not afraid to mention how they believe.

    At all times follow your heart.

  8. Anitra

    I’m gone to say to my little brother, that he should also visit this webpage on regular basis
    to obtain updated from most up-to-date information.

  9. Dewitt

    Hmm is anyone else encountering problems with the images on this blog loading?

    I’m trying to determine if its a problem on my end or if
    it’s the blog. Any feedback would be greatly
    appreciated.

  10. Dominik

    Can I simply just say what a comfort to uncover someone who genuinely knows what they’re talking about on the internet.
    You actually understand how to bring an issue to light and make it important.
    A lot more people really need to check this out
    and understand this side of your story. I can’t
    believe you are not more popular because you certainly
    have the gift.

  11. Tracey

    Hi there! I just wanted to ask if you ever have any issues
    with hackers? My last blog (wordpress) was hacked and I ended up losing a few months
    of hard work due to no back up. Do you have any solutions to protect
    against hackers?

  12. Arnoldo

    You’ve made some really good points there. I checked on the net to
    learn more about the issue and found most individuals will go along with your views on this site.

  13. Kai

    Hmm it looks like your blog ate my first comment (it was
    super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.

    I as well am an aspiring blog writer but I’m still new to everything.
    Do you have any recommendations for inexperienced blog writers?

    I’d definitely appreciate it.

  14. private insta viewer

    Hey there, I think your blog might be having browser compatibility issues.

    When I look at your blog in Firefox, it looks fine but when opening
    in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, fantastic
    blog!

  15. Stacie

    Hi there exceptional website! Does running a blog such as this
    require a great deal of work? I have very little knowledge of
    coding but I was hoping to start my own blog soon. Anyway,
    should you have any suggestions or tips for new blog owners please share.
    I understand this is off subject however I just needed to ask.
    Kudos!

  16. free v bucks apk download

    I love your blog.. very nice colors & theme.
    Did you create this website yourself or did you
    hire someone to do it for you? Plz answer back as I’m
    looking to create my own blog and would like to know where u got this from.
    thank you

  17. Andrew

    I love what you guys are up too. This sort of clever work and reporting!
    Keep up the very good works guys I’ve added you guys to my
    personal blogroll.

  18. Poppy

    It is appropriate time to make some plans for the future and
    it is time to be happy. I have read this post and if
    I could I wish to suggest you some interesting things or
    advice. Maybe you can write next articles referring to
    this article. I desire to read even more things about it!

  19. Darrel

    At this time it looks like Expression Engine is the best blogging platform
    available right now. (from what I’ve read) Is that what you’re using on your blog?

  20. why not try these out

    In now time, considering the fast chosen lifestyle that everyone leads, credit cards get this amazing demand throughout the economy. Persons from every field are using the credit card and people who aren’t using the credit cards have prepared to apply for even one. Thanks for sharing your ideas in credit cards.

  21. Pearlie

    My wife and i were really more than happy when Albert could carry out his basic research from your precious recommendations he received out of your site. It is now and again perplexing to simply choose to be making a gift of guidelines that many other people may have been making money from. And we all realize we have the blog owner to appreciate because of that. Most of the illustrations you made, the simple site menu, the relationships you will aid to engender – it’s all impressive, and it’s assisting our son in addition to our family recognize that the idea is fun, and that’s exceptionally mandatory. Thanks for all the pieces!

  22. funny post

    In these days of austerity and also relative anxiousness about taking on debt, most people balk against the idea of utilizing a credit card to make acquisition of merchandise or maybe pay for a vacation, preferring, instead to rely on this tried and trusted means of making payment – hard cash. However, if you have the cash there to make the purchase in whole, then, paradoxically, that’s the best time to use the credit cards for several factors.

  23. click to investigate

    In these days of austerity plus relative stress about incurring debt, some people balk resistant to the idea of using a credit card in order to make acquisition of merchandise or maybe pay for any occasion, preferring, instead to rely on the tried as well as trusted procedure for making repayment – hard cash. However, if you have the cash there to make the purchase fully, then, paradoxically, that’s the best time just to be able to use the credit card for several factors.

  24. try this out bizopsbusiness.com

    Thanks for these tips. One thing I additionally believe is that credit cards providing a 0 rate often attract consumers along with zero monthly interest, instant authorization and easy on the web balance transfers, but beware of the number one factor that will certainly void your own 0 easy streets annual percentage rate and as well as throw you out into the terrible house quickly.

  25. recommended you read bizopsbusiness.com

    These days of austerity and also relative anxiety about running into debt, many individuals balk contrary to the idea of utilizing a credit card to make purchase of merchandise or maybe pay for any occasion, preferring, instead to rely on the particular tried in addition to trusted way of making settlement – raw cash. However, in case you have the cash available to make the purchase in full, then, paradoxically, that’s the best time for them to use the cards for several causes.

  26. official site bizopsbusiness.com

    One thing is that one of the most widespread incentives for utilizing your credit card is a cash-back or rebate supply. Generally, you will get 1-5 back in various buying. Depending on the credit card, you may get 1 returning on most expenditures, and 5 back on expenditures made from convenience stores, gas stations, grocery stores in addition to ‘member merchants’.

  27. redirected here bizopsbusiness.com

    Thanks for these tips. One thing I also believe is always that credit cards offering a 0 apr often bait consumers in with zero rate, instant acceptance and easy internet balance transfers, but beware of the real factor that will probably void the 0 easy neighborhood annual percentage rate and to throw anybody out into the poor house quick.

  28. click over here bizopsbusiness.com

    Now, with all the fast lifestyle that everyone leads, credit cards get this amazing demand in the economy. Persons from every area are using the credit card and people who not using the credit cards have made arrangements to apply for even one. Thanks for spreading your ideas in credit cards.

  29. click here for info bizopsbusiness.com

    Thanks for these pointers. One thing I additionally believe is that credit cards offering a 0 rate often entice consumers with zero interest, instant authorization and easy on the net balance transfers, however beware of the most recognized factor that will void your current 0 easy street annual percentage rate and to throw one out into the very poor house rapid.

  30. recommended site bizopsbusiness.com

    Thanks for your tips. One thing really noticed is that banks as well as financial institutions know the spending behaviors of consumers and understand that many people max out their credit cards around the getaways. They properly take advantage of this real fact and then start flooding your inbox in addition to snail-mail box with hundreds of no-interest APR card offers soon after the holiday season finishes. Knowing that when you are like 98 of the American general public, you’ll get at the chance to consolidate financial debt and switch balances to 0 interest rates credit cards.

  31. go to these guys bizopsbusiness.com

    My spouse and i ended up being so more than happy Michael could conclude his investigations out of the ideas he obtained while using the weblog. It’s not at all simplistic to simply happen to be giving freely key points which often other people have been trying to sell. And we recognize we need you to thank for this. The specific illustrations you’ve made, the simple website navigation, the relationships your site assist to create – it’s all incredible, and it’s assisting our son and us consider that this matter is fun, which is truly vital. Thank you for the whole lot!

  32. Lauren Thier

    Thanks for these guidelines. One thing I should also believe is that credit cards giving a 0 rate often attract consumers with zero interest rate, instant approval and easy on-line balance transfers, nevertheless beware of the real factor that will void that 0 easy street annual percentage rate plus throw one out into the bad house quick.

  33. Simon Handren

    I truly love your blog.. Excellent colors & theme. Did you create this amazing site yourself? Please reply back as I’m trying to create my very own site and would love to know where you got this from or what the theme is named. Cheers!

  34. Tony Andebe

    I’m pretty pleased to discover this web site. I want to to thank you for your time for this particularly wonderful read!! I definitely appreciated every little bit of it and I have you bookmarked to look at new things in your web site.

Leave a Reply

Your email address will not be published.