Data Format Manipulation in DataWeave 2.0 (Mule 4) — Using Coercing, Custom Data Types, Functions, and Flows
Author: Sanket Kangle
In addition to configuring the output structure, we also might want to format the data. One of the ways to format data is to change the data type. This is called Coercion. To coerce a property to another data type, use the “as” directive followed by the target type and optionally properties that define class or format. There are many types available in DataWeave, From the top-level type “Any” which can receive any type of data from scaler data type like number, string or boolean to more complex data types such as arrays, objects and range and even 7 types to represents dates.
Some of these are as follows:
Let us see an example of using “as” operator for type coercion.
Use the metadata format schema property to format numbers and dates. For example,
In addition to these standard data types, We can create our own custom data-types. We can define custom type data-types using the “type” header directive by providing a name and assigning the coercion properties to it. We can then use the custom data-type to coerce the data anywhere in the body. It is recommended to lead the name of custom data-type in uppercase letter and No special character is allowed. Look at the example below.
We can also use java classes to coerce data or define custom types. Using the fully qualified class name. We can specify inline as shown in the following exhibit.
or by defining custom data type as below:
Data manipulation can also be done using DataWeave functions. Functions are packaged in modules. Functions in the core module are imported automatically into DataWeave scripts. There are two types of syntax can be used to call the function. In the following example, the function used is “contains” and two variables “payload” and “max” are used.
This second type of notation can make calling functions that have a lambda expression as a parameter easier to read. For example, look at the following exhibit
Both notations have the same output but the second one is comparatively easier to read and understand.
It should be kept in mind that when using a series of functions, the first function in the chain is executed first. Look at the following exhibit
In the above example, “flights” variable is filtered to remove any objects with 30 available seats or less then the remaining objects are sorted by their “price”.
Other functions or modules(other than core)need to be imported before using them in the script. Find the example of the importing function in the following exhibit.
Instead of importing a specific function, we can also import a complete module, but in that case, while referencing we need to include the name of the package as well as can be seen in the following exhibit.
We can execute the flow from within a DataWeave script. It is similar to using a flow reference. It is done by using “lookup” function. The lookup function enables us to execute another flow within our application and retrieve the resulting payload. It takes two parameters, the first parameter is the flows’ name and the second parameter is an input payload that is sent to the flow as a map. This may be useful if we need to transform the payload before sending it to the flow for processing.
It is important to keep in mind that the lookup function can only call the flows and not the subflows.
The other ways to do the same is using flow reference to call the flow and use a target attribute to put the result of a variable and reference that variable from inside the DataWeave script.