---
schema_version: "1"
id: 2b7f7e1297fd82f78f6a52978734e0cbb95ecb650d7269b4646d0ede130cad5a
title: Migrating MEL to DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.3/migration-mel.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.3/migration-mel
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 699869c37da32d4f6c63d8a8a3e6bb245eb6e36be20c6c789425e7f68544c4c8
description: MuleSoft Documentation Site
release: "4.3"
latest: false
---
---
product: Mule Runtime
version: 4.3
---

# Migrating MEL to DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

In Mule 4, the DataWeave expression language replaces Mule Expression Language (MEL). This section
provides guidance on migrating MEL expressions in your Mule apps to DataWeave (2.0 and later).

## DataWeave at a Glance

DataWeave is a Mule-specific language that can be used lightly as an expression language to simply
reference values and evaluate conditions, or as a scripting language to construct complex data
transformations that include functions and recursion.

Unlike MEL, it supports different data types out of the box, like JSON, XML, or CSV, which means
data no longer needs to be transformed before expressions can be evaluated. It also supports
advanced use cases like mapping functions or filtering data.

It’s important to notice that the main data selector in DataWeave is the `.` operator. Since it
shares most keywords with MEL, the syntax for most scenarios is identical. For example,
`#[message.payload.name]` is the same in both languages.

The main difference between MEL and DataWeave is that DataWeave expressions have no side effects.
You can use DataWeave to extract or generate data but not to modify it. The
[Java Integration](https://docs.mulesoft.com/mule-runtime/4.3/intro-java-integration) examples explain how to deal with those use cases.

## MEL Usages

The next sections show how to adapt some uses of MEL to Mule 4.

### Accessing Context Variables

Except for the following changes, context variables (also called Mule Runtime variables) remain the
same in DataWeave:

<table><colgroup><col> <col></colgroup><thead><tr><th>MEL</th><th>DataWeave</th></tr></thead><tbody><tr><td><div><div><p><strong>Flow variables</strong></p></div><div><p><code>flowVars</code></p></div><div><ul><li><p>Example: <code>#[flowVars.myVar]</code></p></li></ul></div></div></td><td><div><div><p><strong>Mule Event Variables</strong></p></div><div><p><code>vars</code></p></div><div><ul><li><p>Example: <code>#[vars.myVar]</code></p></li></ul></div></div></td></tr><tr><td><div><div><p><strong>Exception</strong></p></div><div><p><code>exception</code></p></div></div></td><td><div><div><p><a href="https://docs.mulesoft.com/mule-runtime/4.3/error-handling">Mule Error</a>:</p></div><div><p><code>error</code></p></div></div></td></tr><tr><td><div><div><p><strong>Inbound Attachments</strong></p></div><div><p><code>message.inboundAttachments</code></p></div></div></td><td><div><div><p><strong>Multipart Data Formats</strong></p></div><div><p>See:</p></div><div><ul><li><p><a href="https://github.com/mulesoft/mule-migration-assistant/blob/master/docs/user-docs/migration-tool-post-mig.adoc#inbound_attachments">Inbound Attachments (on GitHub)</a></p></li><li><p><a href="https://docs.mulesoft.com/mule-runtime/4.3/intro-mule-message#attachments">Attachments</a></p></li></ul></div></div></td></tr><tr><td><div><div><p><strong>Inbound Properties</strong></p></div><div><p><code>message.inboundProperties</code></p></div><div><ul><li><p>Examples:</p><div><div><pre><code data-lang="txt">#[inboundProperties.'http.query.params']
#[inboundProperties]</code></pre></div></div></li></ul></div></div></td><td><div><div><p><strong>Mule Message Attributes</strong> (metadata)</p></div><div><p><code>attributes</code></p></div><div><ul><li><p>Examples:</p><div><div><pre><code data-lang="txt">#[attributes.queryParams]
#[attributes.headers]</code></pre></div></div></li></ul></div><div><p><a href="https://docs.mulesoft.com/mule-runtime/4.3/intro-mule-message#inbound-properties-are-now-attributes">Inbound Properties Are Now Attributes</a></p></div></div></td></tr><tr><td><div><div><p><strong>Outbound Attachments</strong></p></div><div><p><code>message.outboundAttachments</code></p></div></div></td><td><div><div><p><strong>Multipart data formats</strong></p></div><div><p>See:</p></div><div><ul><li><p><a href="https://github.com/mulesoft/mule-migration-assistant/blob/master/docs/user-docs/migration-tool-post-mig.adoc#outbound_attachments">Outbound Attachments</a></p></li><li><p><a href="https://docs.mulesoft.com/mule-runtime/4.3/intro-mule-message#attachments">Attachments</a>.</p></li></ul></div></div></td></tr><tr><td><div><div><p><strong>Outbound Properties</strong></p></div><div><p><code>message.outboundProperties</code></p></div></div></td><td><div><div><p><strong>Removed:</strong></p></div><div><p>See <a href="https://docs.mulesoft.com/mule-runtime/4.3/intro-mule-message#outbound-properties">Outbound Properties</a>.</p></div></div></td></tr><tr><td><div><div><p><code>message.dataType</code></p></div></div></td><td><div><div><p><code>dataType</code></p></div></div></td></tr><tr><td><div><div><p><code>message.id</code>, <code>message.rootId</code>, <code>message.replyTo</code></p></div></div></td><td><div><div><p><strong>Removed</strong></p></div></div></td></tr><tr><td><div><div><p><strong>Record Variables</strong></p></div><div><p><code>recordVars</code></p></div><div><ul><li><p>Example: <code>#[recordVars.myVar]</code></p></li></ul></div></div></td><td><div><div><p><strong>Mule Event Variables</strong></p></div><div><p><code>vars</code></p></div><div><ul><li><p>Example: <code>#[vars.myVar]</code></p></li></ul></div></div></td></tr><tr><td><div><div><p><strong>Session Variables</strong></p></div><div><p><code>sessionVars</code></p></div><div><ul><li><p>Example: <code>#[sessionVars.myVar]</code></p></li></ul></div></div></td><td><div><div><p><strong>Removed:</strong></p></div><div><p>Transport barriers do not exist in Mule 4. See:</p></div><div><ul><li><p><a href="https://docs.mulesoft.com/mule-runtime/4.3/intro-mule-message#session-properties">Session Properties</a></p></li></ul></div></div></td></tr><tr><td><div><div><p><code>server.dateTime</code></p></div></div></td><td><div><div><p><strong>Removed:</strong></p></div><div><p>Use the DataWeave <a href="https://docs.mulesoft.com/dataweave/latest/dw-core-functions-now"><code>now</code></a> function.</p></div></div></td></tr></tbody></table>

### Extracting Data

You can use [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.3/dataweave-selectors) to query data as you did
with MEL. Only now, you won’t need extra transformations when dealing with types like JSON.

The next examples log the ID from this JSON payload:

JSON Payload

```json
{
  "name": "Rachel Duncan",
  "id": "779H41"
}
```

Mule 3 Example

```xml
<json:json-to-object-transformer returnClass="java.util.Map" />
<logger message="Updating health check record for subject '#[payload.id]'" level="INFO" />
```

Mule 4 Example

```xml
<logger message="Updating health check record for subject '#[payload.id]'" />
```

### Assigning Values

As mentioned earlier, DataWeave does not support modifying data in the way the
`expression-component` allowed with MEL. To achieve those use cases, you must use the Scripting
module.

Consider payload described [above](#extract_data) as a Java Map. The examples modify the `name`
attribute.

Mule 3 Example

```xml
<expression-component>
  <![CDATA[
    payload.name = 'R. Duncan'
  ]]>
</expression-component>
```

Mule 4 Example

```xml
<script:execute engine="groovy">
  <script:code>
    payload.put('name', 'R. Duncan')
    return payload
  </script:code>
</script:execute>
```

To use the Scripting module, simply add it to your app using the Studio palette, or add the
following dependency in your `pom.xml` file:

```xml
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-scripting-module</artifactId>
  <version>1.1.0</version> <!-- or newer -->
  <classifier>mule-plugin</classifier>
</dependency>
```

### Invoking Java Methods

DataWeave can be used to invoke static methods. For regular methods, you can use the Java module,
which exposes both an operation and a function to invoke Java methods.

#### Static Methods

The following examples use Java’s Locale `getDefault` static method in Mule 3 and 4.

Mule 3 example

```xml
<validation:is-time time="#[payload]" pattern="h:mm a" locale="#[java.util.Locale.getDefault().getLanguage()]"/>
```

Mule 4 example

```xml
<validation:is-time time="#[payload]" pattern="h:mm a" locale="#[java!java::util::Locale::getDefault().getLanguage()]"/>
```

Note that DataWeave requires the `java!` prefix to indicate that a Java static method is to be
searched, and it requires the fully qualified Class name (separated by `::` instead of `.`).

#### Instance Methods

The following examples show the usage of Java’s String `equalsIgnoreCase` method in Mule 3 and 4.

Mule 3 example

```xml
<choice>
  <when expression="#[payload.equalsIgnoreCase('error')]">
    <logger message="An error message has been received." level="ERROR"/>
  </when>
  <otherwise>
    <logger message="Message received: #[payload]" level="INFO"/>
  </otherwise>
</choice>
```

Mule 4 example

```xml
<choice doc:name="Choice">
  <when expression="#[Java::invoke('java.lang.String', 'equalsIgnoreCase(String)', payload, {arg0: 'error'})]">
		<logger message="An error message has been received." level="ERROR"/>
	</when>
	<otherwise >
		<logger message="Message received: #[payload]" level="INFO"/>
	</otherwise>
</choice>
```

To use the Java module, simply add it to your app using the Studio palette, or add the following
dependency to your `pom.xml` file:

```xml
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-java-module</artifactId>
  <version>1.0.0</version> <!-- or newer -->
  <classifier>mule-plugin</classifier>
</dependency>
```

### Target Definitions

The `enricher` has been removed and replaced by target variables, which are now supported by every
operation. The example below shows how to send the result of an operation to a variable in Mule 3
and 4.

Mule 3 example

```xml
<enricher target="#[flowVars.response]">
    <http:request config-ref="HTTP_Request_Configuration" path="/get" method="GET"/>
</enricher>
```

Mule 4 example

```xml
<http:request config-ref="HTTP_Request_Configuration" path="/get" method="GET" target="response"/>
```

- See [Enriching Data with Target Variables](https://docs.mulesoft.com/mule-runtime/4.3/target-variables) for more details.

### XPath Function

DataWeave can be used to query XML content using its
[selectors](https://docs.mulesoft.com/dataweave/2.3/dataweave-selectors), but you can also use the XML module, which
exposes both an operation and a function to execute XPath queries.

The following examples show how to take lines containing a specific word taken from a variable in
Mule 3 and 4.

Mule 3 example

```xml
<set‐variable variableName="word" value="handkerchief"/>
<expression‐transformer>
   xpath3('//LINE[contains(.,$word)]',payload,'NODESET')
</expression‐transformer>
```

Mule 4 example

```xml
<set‐variable variableName="word" value="handkerchief"/>
<set-payload value="#[XmlModule::xpath('//LINE[contains(., \$word)]', payload, {'word': vars.word})]" />
```

To use the XML module, simply add it to your app using the Studio palette, or add the following
dependency to your `pom.xml` file:

```xml
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-xml-module</artifactId>
  <version>1.1.0</version> <!-- or newer -->
  <classifier>mule-plugin</classifier>
</dependency>
```

### Wildcard and Regex functions

DataWeave matching functions `match` and `matches` (see Core DataWeave Functions) can be used
instead. The next example shows how a regex is used in DataWeave to replace the use of the
`wildcard` MEL function.

Mule 3 example

```xml
<choice>
  <when expression="#[wildcard('Hello *')]">
    <set-payload value="Hello, how can I help?"/>
  </when>
  <otherwise>
    <set-payload value="Courtesy requires a greeting."/>
  </otherwise>
</choice>
```

Mule 4 example

```xml
<choice doc:name="Choice">
  <when expression="#[payload matches /Hello\s[A-z]+/]">
    <set-payload value="Hello, how can I help?"/>
  </when>
  <otherwise >
    <set-payload value="Courtesy requires a greeting."/>
  </otherwise>
</choice>
```

## See Also

[DataWeave Language](https://docs.mulesoft.com/mule-runtime/4.3/dataweave)

[Why DataWeave is the Main Expression Language in Mule 4 Beta](https://blogs.mulesoft.com/dev-guides/how-to-tutorials/why-dataweave-main-expression-language-mule-4/)
