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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Import a Function/Module in Dataweave
  • Java String: A Complete Guide With Examples
  • Proper Java Exception Handling

Trending

  • Top Book Picks for Site Reliability Engineers
  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Java Virtual Threads and Scaling
  • Develop a Reverse Proxy With Caching in Go
  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 user avatar
Satheesh Kumar
·
Jun. 06, 17 · Opinion
Likes (9)
Comment
Save
Tweet
Share
84.3K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Java String: A Complete Guide With Examples
  • Proper Java Exception Handling

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!