Posts

Showing posts with the label DataWeave

How to Modularize your custom DataWeave 2.0 scripts

Image
     DataWeave is the main expression language in Mule 4, and with the recent version 2.0, it is the sole transformation language in a Mule 4 application. DataWeave is a powerful and rich scripting language, which  supports a variety of useful functions for data manipulations and transformations. It employs a functional programming principle, which means that functions don't have states and side effects. DataWeave functions can also be called recursively (I have done some recursive functions before but will think of a simple concrete example).     If you want to read and study more about DataWeave, and familiarize yourself with the different supported functions, go to  https://docs.mulesoft.com/mule-runtime/4.2/dw-functions     The DataWeave related functions are organized into modules. There are core modules, string modules, binary, cryptography, and a lot more. DataWeave is quite extensible, and I think in future upgrades of Mule we will se...

Global Function that can be used in Mule DataWeave

You can write a Groovy custom function that can be used in Mule DataWeave (Transform Message). To do this, create a file with .mvel extension under src/main/resources, e.g. globalfunction.mvel. If you're familiar with Java, it is quite similar to Groovy, but with some differences such as type declaration is not required in Groovy and a statement doesn't have to be punctuated with a semi-colon as long as you write one statement per line. For example, we have the following Groovy function, which accepts a MongoDB ObjectId and converts it into a string value. This is useful when you just need to get the hexadecimal value and not the object itself, which gets serialized when transforming to JSON. def getObjectIdAsString(objectId) {     return ((org.bson.types.ObjectId) objectId).toString() }    In the Mule configuration file, add the below XML. You don't need to specify the complete path since src/main/resources automatically gets added to the classpat...