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
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
View Events Video Library
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation
  • Using Regular Expressions in Python: A Brief Guide
  • Ultimate Guide to FaceIO

Trending

  • Understanding Zero Trust Security Building a Safer Digital World
  • Python “Magic” Methods (Part 2)
  • What Is SQL Vector Database?
  • The Efficiency of ELT Over ETL
  1. DZone
  2. Coding
  3. JavaScript
  4. Mule 4 DataWeave Functions: Part 1

Mule 4 DataWeave Functions: Part 1

Let's take a look at this tutorial, which gives an explanation on how to import modules by adding the import directive to the head of the DataWeave script.

Murali Thuraka user avatar by
Murali Thuraka
·
Aug. 06, 18 · Tutorial
Like (9)
Save
Tweet
Share
41.7K Views

Join the DZone community and get the full member experience.

Join For Free

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 the 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:

We will proceed with the rest of the functions in the Mule 4 DataWeave Functions: Part 2 article.

Payload (computing) Diff Filter (software) MOD (file format) Strings Directive (programming) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation
  • Using Regular Expressions in Python: A Brief Guide
  • Ultimate Guide to FaceIO

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
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: