Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

10 Quick Dataweave Functions

DZone's Guide to

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.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements. What's below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

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."

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
mulesoft ,dataweave ,esb ,cloudhub ,function composition

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}