Dataweave 2.0 — Introduction to Transformations

Sanket Kangle

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

First, let us see what is a Mule event: A Mule event contains the core information processed by the runtime. It travels through components inside your Mule app following the configured application logic.

Note that the Mule event is immutable, so every change to an instance of a Mule event results in the creation of a new instance.

A Mule Event is composed of these objects:

  • A Mule Message contains a message payload and its associated attributes.
  • Variables are Mule event metadata that you use in your flow.

DataWeave scripts act 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.

DataWeave supports many formats. Some of the most commonly used formats are Java, JSON, XML, dw(dataweave — for testing a DataWeave expression), CSV & xlsx. The DataWeave expression is a data model for the output. It is not dependent on the types of input and output, just their structure.

Note: The examples in this article are worked on Anypoint Studio 7.7 & runtime used is Mule Server 4.3.0 EE

The most complex transformations are performed in the “Transform Message” processor. It can be drag-and-dropped to the canvas from the Core module as shown in the below exhibit.

In the properties tab of transform message, the output type is set to application/java by default as Mule, the runtime engine of Anypoint Platform is a lightweight Java-based enterprise service bus (ESB).

Until the target is expecting any other format such as JSON or XML, we should leave this to Java only. It ensures no internal marshaling and unmarshalling is done hence giving better performance.

To change the output datatype, we just need to change the application/java to the required data type. Let us see an example where we will change JSON input into different output data formats

Note that before importing any data type, in the Package Explorer, You do not have examples or schema section

It is automatically created and populated once you import an example or schema of data as shown in the below exhibit

Basic Data Structures Examples

In JSON, objects are denoted with curly braces, an array with square braces, and properties are written as key-value pairs delimited by colon and separated by a comma.

When writing an expression for XML input, by default, only XML elements and not attributes are created as JSON fields or Java object properties. We need to use the “@” sign to reference the attributes as shown in the exhibit above.

  • Output in type JAVA

In the input section, we can check out the data type and format, we can check a preview of output by clicking on the preview button in the upper right corner(This will work only if you have imported an example while defining data-type in the input section of Transform Message component)

  • Output in type JSON

Just changing /java to /json will convert the output in JSON format which can be verified using the preview tab

  • Output in type XML

XML format has to have a root object. So just by putting /xml instead of now /json, we get the following error as we do not have a root element.

After adding the root object, we get the desired output in the preview section.

If we want to add any attribute to the target output, then we can add it as shown in the following exhibit. here attribute currency = USD is added to the price.

  • Output in CSV format:

For CSV type output, there should not be a root object nor a nested object (such as ‘plane’ in the above example).

Types of DataWeave Errors

There are two types of DataWeave errors:

  1. Scripting Errors: It is a problem with the syntax of DataWeave Expression. These are comparatively easy to handle.
  2. Formatting Errors: It is a problem with how the transformation from one format to another is written. For example, A script to output XML that does not specify a data structure with a single root node.

If you get an error, transform the input to application/dw, if the transformation is successful, then the error is likely a formatting error.