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.
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.
Additionally, you can sum up values in an array, or list of values by using the 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.
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.
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.
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.
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.
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.
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.