Contact Us 1-800-596-4880

MEL DataWeave functions

The Mule Expression Language (MEL) includes two functions that take advantage of the powerful DataWeave transformation language, ideal for carrying out complex data transformations.

All components in Mule that support Mule Expression Language also support expressions written in DataWeave Language.

The DataWeave language is easiest to use by adding a Transform Message Component to your flow, as it offers a graphical UI to build automatic mappings through a drag-and-drop interface, as well as rich debugging features. See About Transform Message Component for more details.

Prerequisites

This document assumes you are familiar with both Mule Expression Language and DataWeave Language.

The DW Function

To invoke an expression written in DataWeave language, simply invoke the dw() function, the expression will return whatever the transform outputs.

DataWeave expressions defined within this function work just as those defined within a Transform Message element, the only difference is that the output is returned into the expression’s result, wherever it may be.

The DataWeave expression that you write in this function must be enclosed in "quotation marks"

For example, you can define a custom object and populate it with elements from the payload:

dw("myobject:{id:payload.accountid, user:payload.user}")

That same expression could be added inside a Logger, within a MEL expression, to print out its result:

#[dw("myobject:{id:payload.accountid, user:payload.user}")]

Full attribute reference for the function:

Attributes Description

DataWeave Expression

The actual DataWeave expression to carry out

Output Type

The type of the output of the transform

Reader Properties

For inputs such as CSV, sometimes some properties are needed to know how to parse the input, see To Define Input and Output Structure of a Transformation

Writer Properties

For inputs such as CSV, sometimes some properties are needed to compose the output, see To Define Input and Output Structure of a Transformation

Reader and writer properties are only supported on versions Mule Runtime 3.8.2 and newer.

Below is a more elaborate example that uses all the attributes:

#[dw(payload.script,'application/xml', ['payload': ['nullValueOn':'empty']], ['output': ['indent':'true']])]

In the example above, the first attribute is a simple DW expression that references an element in the payload, the second tells DataWeave to output this as 'application/xml', the third includes a map with reader properties, the fourth includes a map with writer properties.

The maps for reader and writer properties should follow this structure:

  • The first key defines the target of the configuration, the value for this key is another map

  • Within this map, you list key value pairs for the features to configure

['output': ['indent':'true']]
See DataWeave Formats for a reference of all the different properties you can set for reader and writer. The available options vary depending on the format.

The Split Function

The split() function runs an expression written in DataWeave language and returns an iterator the result of which can then be processed by your flow one index at a time. It takes at least two arguments: a DataWeave expression and the desired output type. This function may be equivalent to running the 'dw()' function and then adding an additional step to your flow, such as a Foreach Scope, but it’s less heavy on resources.

The DataWeave expression that you write in this function must be enclosed in "quotation marks" (either single quotation marks ' or double quotation marks ". The result of the split function must also return a single array.

Below is the most basic DW expression you can write, but you could also put any other DW expression as long as it returns an iterable list.

split('payload','application/json')

That same expression could be added inside a Foreach Scope in the Collection field, to use this expression to parse the payload into a separate set of messages to process individually (one at a time):

<flow name="myFlow">
    <foreach collection="split('payload','application/json')" doc:name="For Each">
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </foreach>
</flow>

The function has the following attributes:

Attributes Description

DataWeave Expression

The actual DataWeave expression to carry out

Output Type

The type of the output of the transform

Reader Properties

For inputs such as CSV, sometimes some properties are needed to know how to parse the input, see To Define Input and Output Structure of a Transformation

Writer Properties

For inputs such as CSV, sometimes some properties are needed to compose the output, see Output Directive