Data format manipulation in DataWeave 2.0 (Mule 4)
- December 15, 2020
In addition to configuring the output structure, you may also 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, any properties that define class or format.
DataWeave has many available types. These range from the top-level “any,” which can receive any type of data — from scaler data such as number, string or Boolean — to more complex data types such as arrays, objects, range and even seven types to represent dates.
Some of these are as follows:
Here’s an example of using “as” operator for type coercion:
Use the metadata format schema property to format numbers and dates. For example:
Custom data type
In addition to these standard data types, you can create your own custom data types by using the “type” header directive, and then providing a name and assigning coercion properties to it. Custom data types can coerce data anywhere in the body.
It’s recommended that the name of your custom data type start with an uppercase letter. Special characters aren’t allowed.
For example:
You can use Java classes to coerce data or define custom types using the fully qualified class name, as shown below:
Or by defining a custom data type like this:
Functions
You can use DataWeave functions to manipulate data. Functions are packaged in modules. Functions in the core module are imported automatically into DataWeave scripts. Two types of syntax can be used to call the function. In the following example, the function is “contains” and the two variables are “payload” and “max”:
This second type of notation can make calling functions that have a lambda expression as a parameter easier to read. For example:
Both notations have the same output, but the second one is comparatively easier to read and understand.
Keep in mind that when using a series of functions, the first function in the chain is executed first. For example:
In the above example, the “flights” variable is filtered to remove any objects with 30 available seats or less. Then, the remaining objects are sorted by “price.”
You must import other functions or modules (other than core) before using them in the script.
Here’s an example of the importing function:
Instead of importing a specific function, you can also import a complete module. In that case, however, you must include the name of the package, as shown in the following example:
Flow
You can execute the flow from within a DataWeave script. As in a flow reference, you use the “lookup” function to execute another flow within the application and retrieve the resulting payload. It takes two parameters. The first parameter is the name of the flows; the second parameter is an input payload sent to the flow as a map. This may be useful if you need to transform the payload before sending it to the flow for processing.
It’s important to keep in mind that the lookup function can only call the flows, not the subflows. You can also use a flow reference to call the flow or a target attribute to insert the result of a variable and reference that variable from inside the DataWeave script.
— By Sanket Kangle