Data Format Manipulation in DataWeave 2.0 (Mule 4) — Using Coercing, Custom Data Types, Functions, and Flows

Sanket Kangle
5 min readJun 5, 2021
Photo by Mae Mu on Unsplash

Coercing

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 scalar data types 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:

Any, Array, Binary, Boolean, CData, Comparable, Date and Time, Dictionary, Enum, Iterator, Key, Namespace, Nothing, Null, Number, Object, Range, Regex, SimpleType, String, TryResult, Type, Uri

Let us see an example of using “as” operator for type coercion.

price: payload.price as Number
price: $.price as Number {class:”java.lang.Double”}

Use the metadata format schema property to format numbers and dates. For example,

price as Number as String {format: “###.00”}
someDate as DateTime {format: “yyyyMMddHHmm”}

Custom Data-Type

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:

Functions

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 that can be used to call the function. In the following example, the function used is “contains” and two variables “payload” and “max” are used.

#[contains(payload, max)]#[payload contains “max”]

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 that 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 than 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.

Flow

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.

Give claps if you found this article useful. Stay tuned for upcoming articles.

All the images/exhibits are from the author unless given credit

Thanks for reading the article! Wanna connect with me?
Here is a link to my Linkedin Profile

--

--