Contact Free trial Login

DataWeave Language

The simple example below demonstrates a few important features of the DataWeave language:

  • Data transformation from one format to another. The example transforms application/json input to application/xml output.

  • Data selectors that access fields within an input data structure. The script selects keys in a JSON object from the message payload input (for example, with payload.title) and then outputs their values.

  • Use of a core DataWeave function on the value of an input field upper(payload.author).

Before you begin, note that DataWeave 2.0 is for Mule 4 apps. For a Mule 3 app, refer to the DataWeave 1.0 documentation set in the Mule 3.9 documentation. For other Mule versions, you can use the version selector for the Mule Runtime table of contents.

Input Payload
{
  "title": "Java 8 in Action",
  "author": "Mario Fusco",
  "year": 2014
}
Transform
%dw 2.0
output application/xml
---
{
  order: {
    'type': 'Book',
    'title': payload.title,
    'author': upper(payload.author),
    'year': payload.year
  }
}
Output Payload
<?xml version='1.0' encoding='UTF-8'?>
<order>
  <type>Book</type>
  <title>Java 8 in Action</title>
  <author>MARIO FUSCO</author>
  <year>2014</year>
</order>

Far more complex data manipulation and transformations are possible.

DataWeave supports several file input and output formats in addition to XML and JSON. It provides a number of functions for manipulating data and includes selectors for accessing fields in the data structure. It can handle a number of data types in addition to arrays, key-value pairs, Java objects, strings, and numbers. It also supports type coercion, and the creation and use of your own functions, data types, and variables in your scripts.

You write DataWeave expressions and scripts within message processors (components, connectors, or modules) when creating apps in Anypoint Studio or Design Center, or as handwritten XML configuration files.

  • The Transformer component is for creating scripts that perform data transformations, whether simple format conversions or complex data extraction and transformation processes.

  • Many Mule message processors support DataWeave expressions, allowing you to access and use core DataWeave functions on parts of the Mule event that you need.

    See the inline DataWeave script examples for Set Payload in DataWeave Scripts. For other examples, see the message attribute in Logger Component examples, Set Payload (<ee:set-payload>) and Set Variable components in the For Each Scope examples, and <file:content> in the File Connector documentation.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.