Skip to main content
Contact Us 1-800-596-4880

Getting started with DataWeave: Part III

5 min read

Getting started

If you haven’t read the first part of our Getting Started with Mapping & Filters in DataWeave, click the link to learn how to build simple transformations using the DataWeave language. In this tutorial, we will be diving deep on how simple it is to write custom equations and edit strings using DataWeave.


Math functions

DataWeave comes with a full set of mathematical equations pre-installed into the language. You can use the random() function to generate a random number between 0 and 1.

Random function

Additionally, you can sum up values in an array, or list of values by using the sum() function.

Sum function

Let’s say you have an array of values, and you have to find the square root of the average of those values. Use the avg() function to determine the average value of the payload, however around that use the sqrt() function to take the square root of the average.

Avg function

Numbers (dw::core::Numbers)

The Numbers library enables conversion of numbers into formats such as binary, hex or radix and can be imported into your DataWeave script by including the package in the header of the script. The toHex() function converts a number into hexadecimal, and the fromHex() function converts a hexadecimal number into a decimal number. The toBinary() function transforms a number into Binary, and the fromBinary() function transforms a number from Binary into a decimal number. The toRadixNumber() function transforms a specified radix into a decimal number, and the fromRadixNumber() function transforms a number into a radix. Check out the below demonstration to see these functions working in action.

Numbers

splitBy

The splitBy() function splits a string into an array based on the regex values that match up with the string. This is a powerful function, and you can use it to split up strings, add values inside of each array element, then just use the joinBy() function to rejoin the string from the output array.

splitBy function

Generate UUID

The UUID() function is an important call to generate a unique identifier for users in your application. User accounts should include a universally unique identifier to help distinguish accounts from each other that may share similar characters such as name, email, address etc.

UUID function

Replace

The replace() function is used to replace certain parts of the string with an updated value. The function is split into two parts, the text that the string will search for to match, and the matcher which includes the matching values that the text will replace.

Replace function

Scan

The scan() function returns an array with whatever matches are found in the given input string. Each of the matches is returned in an array and the function uses regex to scan through the string for matches. In the example below, each section of the scan (defined by parathesis), is split up into its own value in the array which makes it easy to modify and then combine the array after the contents have been modified.

Scan function

Conclusion

Thanks for reading our tutorial series on DataWeave. Hopefully, these guides were beneficial in getting started with learning DataWeave. There are plenty of functions that were not listed here, so for the full list of possibilities, please visit the DataWeave API Documentation page. If you have any questions or would like more content covered in these tutorials, reach out to us on Twitter at @muledev.

Previous

Try Anypoint Platform for free

Start your 30-day free trial of the #1 platform for integration, APIs, and automation. No credit card required. No software to install.

Try for free

anypoint product trial zigzag