Nav

About DataWeave

DataWeave is the MuleSoft expression language for accessing and transforming data received through a Mule app. DataWeave is tightly integrated with Mule runtime, which runs the scripts and expressions in your Mule app.

DataWeave acts on data in the Mule event. Most commonly, you use it to access and transform data in the message payload. For example, after a component in your app retrieves data from one system, you can use DataWeave to modify and output selected fields in that data to a new data format, then use another component in your app to pass on that data to another system.

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).

Input Payload

         
      
1
2
3
4
5
{
  "title": "Java 8 in Action",
  "author": "Mario Fusco",
  "year": 2014
}
Transform

         
      
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
output application/xml
---
{
  order: {
    'type': 'Book',
    'title': payload.title,
    'author': upper(payload.author),
    'year': payload.year
  }
}
Output Payload

         
      
1
2
3
4
5
6
7
<?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 About 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.

In this topic: