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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Satheesh Kumar user avatar by
Satheesh Kumar
·
Jun. 06, 17 · Opinion
Like (9)
Save
Tweet
Share
81.83K 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.

Popular on DZone

  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • Spring Boot, Quarkus, or Micronaut?
  • The Path From APIs to Containers

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: