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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Import a Function/Module in Dataweave
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers

Trending

  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • LangChainGo vs Genkit Go: Where Genkit Shines
  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • Evaluating RAG in Production — What the Numbers Actually Revealed

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. 10 Quick Dataweave Functions

10 Quick Dataweave Functions

This quick reference collects many Dataweave functions for MuleSoft in one place, like reversing strings and flattening and trimming a payload.

By 
Satheesh Kumar
·
Jun. 06, 17 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
84.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Since Dataweave is new in MuleSoft, it's hard to identify how to do some basic functions like substring reversing a string. This guide will provide all the functions in one place, which saves developers time. Below is the JSON I am using as a sample payload.

{
"firstName":"satheesh",
"lastName":"Kumar",
"age":"26",
"Amount":10.5
}

1. Substring

Most of us need to use substring somewhere in Dataweave, and it's very simple.We have to use the range operator the range can be mentioned as in array.

 firstName: payload.firstName[0..3]  will give you "firstName": "sath."

2. Reverse a String

Reversing a string is made very easy in Dataweave, so when you have a palindrome or not program next time, you don't need to use Java. You can use it directly in Dataweave.

 firstName: payload.firstName[-1..0]  will give you "firstName": "hseehtas."

3. Replace

To replace a string with another, the replace function is used.

 firstName: (payload.firstName replace "s" with "new")  will give you "firstName": "newatheenewh."

4. Identify the Size of the String

To identify the size of any value, the size of operator is used. It's not only for strings- it's for measuring arrays or lists, too.

 sizeOfFirstName: sizeOf payload.firstName  will give you "sizeOfFirstName": 8.

5. Concat

Concatenation is made very easy in Dataweave; it can be done by just using   + operator .

 Name: payload.firstName ++ " " ++ payload.lastName  will give you  "Name": "satheesh Kumar."

6. Trim

Trimming unwanted spaces is always required in integrations.

When the first name is "       satheesh," use  Name: trim payload.firstName  and it will give you "Name": "satheesh."

7. Other String Operations

I don't need to explain this as the example says everything.

Input Function Sample Result
satheesh capitalize

Name: capitalize payload.firstName

Name:Satheesh
satheesh_mule_developer camelize

Name: camelize payload.firstName

satheeshMuleDeveloper
monday tuesday dasherize

Name: dasherize payload.firstName

monday-tuesday
child

pluralize

Name: pluralize payload.firstName

children
cars singularize

Name: singularize payload.firstName

car
SAtheesh lower

Name: singularize payload.firstName

satheesh
SaTheesh upper

Name: singularize payload.firstName

SATHEESH


8. Flatten

When you have a multiple array list from multiple sources as shown below, Flatten will be very handy.

[{
"firstName":"cars",
"lastName":"Kumar",
"age":"26",
"Amount":10.5
}],
[{
"firstName":"cars",
"lastName":"Kumar",
"age":"26",
"Amount":10.5
}]

 flatten.payload  will give you

[{
"firstName":"cars",
"lastName":"Kumar",
"age":"26",
"Amount":10.5
},
 {
"firstName":"cars",
"lastName":"Kumar",
"age":"26",
"Amount":10.5
}]

9. Floor and Ceil

The Floor function will round down the decimal value into an integer and Ceil will round up the decimal value into an intenger.

"age":26.5

 age: floor payload.age  will give you 26.

 age: ceil payload.age  will give you 27.

10. Now

The Now function will give you the current date and time.

 currentTime: now  will give you "currentTime": "2016-10-20T17:15:06.196Z."

Strings Data Types IT Operator (extension) Data structure JSON Space (architecture) Java (programming language) dev

Opinions expressed by DZone contributors are their own.

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Import a Function/Module in Dataweave
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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