English 中文(简体)
MuleSoft - DataWeave Language
  • 时间:2024-12-22

MuleSoft - DataWeave Language


Previous Page Next Page  

DataWeave is basically a MuleSoft expression language. It is mainly used for accessing and transforming the data received through a Mule apppcation. Mule runtime is responsible for running the script and expressions in our Mule apppcation, DataWeave is strongly integrated with Mule runtime.

Features of DataWeave Language

Following are some important features of DataWeave language −

Data can be transformed from one format to another very easily. For example, we can transform apppcation/json to apppcation/xml. The input payload is as follows −

{
   "title": "MuleSoft",
   "author": " tutorialspoint.com ",
   "year": 2019
}

Following is the code in DataWeave for transform −

%dw 2.0
output apppcation/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>tutorialspoint.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.

Prerequisites

We need to satisfy the following prerequisites before using DataWeave scripts on our computer −

    Anypoint Studio 7 is required to use Dataweave scripts.

    After instalpng Anypoint Studio, we need to set up a project with a Transform Message component in order to use DataWeave scripts.

Steps for Using DataWeave Script with Example

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 −

DataWeave Script

Step 4

Next, in the Transform Message component tab, cpck on Preview to open Preview pane. We can expand the source code area by cpcking the empty rectangle next to Preview.

Step 5

Now, we can start scripting with DataWeave language.

Example

Following is the simple example of concatenating two strings into one −

Transform Message Component

The above DataWeave script is having a key-value pair ({ myString: ("hello" ++ "World") }) which will concatenate two strings into one.

Advertisements