DataWeave is basically a MuleSoft expression language. It is mainly used for accessing and transforming the data received through a Mule application. Mule runtime is responsible for running the script and expressions in our Mule application, DataWeave is strongly integrated with Mule runtime.
Following are some important features of DataWeave language −
Data can be transformed from one format to another very easily. For example, we can transform application/json to application/xml. The input payload is as follows −
{ "title": "MuleSoft", "author": " howcodex.com ", "year": 2019 }
Following is the code in DataWeave for transform −
%dw 2.0 output application/xml --- { order: { 'type': 'Tutorial', 'title': payload.title, 'author': upper(payload.author), 'year': payload.year } }
Next, the output payload is as follows −
<?xml version = '1.0' encoding = 'UTF-8'?> <order> <type>Tutorial</type> <title>MuleSoft</title> <author>howcodex.com</author> <year>2019</year> </order>
The transform component can be used for creating scripts that performs both simple as well as complex data transformations.
We can access and use core DataWeave functions on parts of the Mule event that we need as most of the Mule message processors support DataWeave expressions.
We need to satisfy the following prerequisites before using DataWeave scripts on our computer −
Anypoint Studio 7 is required to use Dataweave scripts.
After installing Anypoint Studio, we need to set up a project with a Transform Message component in order to use DataWeave scripts.
In order to use DataWeave scrip, we need to follow the steps below −
Step 1
First, we need to set up a new project, as we did in the previous chapter, by using File → New → Mule Project.
Step 2
Next, we need to provide the name of the project. For this example, we are giving the name, Mule_test_script.
Step 3
Now, we need to drag the Transform Message component from Mule Palette tab into canvas. It is shown as below −
Step 4
Next, in the Transform Message component tab, click on Preview to open Preview pane. We can expand the source code area by clicking the empty rectangle next to Preview.
Step 5
Now, we can start scripting with DataWeave language.
Following is the simple example of concatenating two strings into one −
The above DataWeave script is having a key-value pair ({ myString: ("hello" ++ "World") }) which will concatenate two strings into one.