DataWeave is a programming language designed for transforming data. It is MuleSoft’s primary language for data transformation, as well as the expression language used to configure components and connectors within mule. DataWeave allows users to easily read and parse data from one format, transform it, and write it out as a different format. For example, a DataWeave script could take a JSON object and transform it into a simple CSV file. It could take in XML, JSON, CSV, excel, binary, java and so on and write the data out to a XML, JSON, CSV, avro, java and so on. DataWeave allows the developer to focus on the transformation logic instead of worrying about the specifics of reading, parsing, and writing specific data formats in a performant way.
DW Script
output application/java
---
{
name: "JSON",
message: "Hello from JSON"
}
Output
{name=JSON, message=Hello from JSON}
Script Anatomy
Let’s start anatomy of a DataWeave script using the below code.
DW Script
%dw 2.0
input payload json
output application/java
---
payload
The first three lines of the script contain directives.
The first directive, which is in every DataWeave file, defines which version the script is using.
Note : If you’re using DW 1, you will always use %dw 1.0. If you’re using DW 2, you will always use %dw 2.0
The second lines contain the input directives. Which allows naming the inputs and defining reader properties.
input <var_name> <mime_type> [<reader_properties>]
Note : The input directive is not required in some scenarios when the DataWeave execution itself is contextualized and the input derived from said context. For example, in Mule their event concept provides the context, adding inputs such as payload and vars.
The third lines contain the output directives. Which allows naming the outputs and defining writer properties.
output <mime_type> [<writer_properties>]
After the first three lines of the script there is a line only containing three dashes. This is to separate your declarations from your script output logic. You can also declare functions and variables that you can reuse in your script.
Finally, the last line of the script is the output section. Whatever the output section evaluates to is what gets sent to the writer, and is ultimately serialized into the specified output format.
DW Script
%dw 2.0
input payload json
output yaml
---
[
{
"firstName": "John",
"lastName": "Smith",
"age": 45
},
{
"firstName": "Jane",
"lastName": "Doe",
"age": 34
}
]
Output
%YAML 1.2
---
-
firstName: John
lastName: Smith
age: 45
-
firstName: Jane
lastName: Doe
age: 34
MIME Types
When it comes to parsing and serializing data, DataWeave does need to be told what data to expect and generate. This is done by specifying MIME types for the inputs and output. We use MIME types to inform DataWeave what data format to read and write.
Name | MIME Type | ID | Supported Formats |
AVRO | application/avro | avro | Avro Format |
CSV | application/csv | csv | CSV Format |
DW | application/dw | dw | DataWeave Format |
FLATFILE | application/flatfile | flatfile | Flat File Format, COBOL Copybook Format, Fixed Width Format |
JAVA | application/java | java | Java Format, Enum Format for Java |
JSON | application/json | json | JSON Format |
BINARY | application/octet-stream | binary | Binary Format |
EXCEL | application/xlsx | excel | Excel Format |
XML | application/xml | xml | XML Format, CData Custom Type |
NDJSON | application/x-ndjson | ndjson | Newline Delimited JSON Format (ndjson) |
UNLENCODED | application/x-www-form-urlencoded | urlencoded | URL Encoded Format |
YAML | application/yaml | yaml | YAML Format |
MULTIPART | multipart/form-data | multipart | Multipart Format |
TEXT | text/plain | text | Text Plain Format |
PROPERTIES | text/x-java-properties | properties | Text Java Properties |
Since DataWeave 2.3, MIME types can be specified with simple IDs such as json or xml.
Input
[
{
"firstName": "Gaurav",
"lastName": "Dubey",
"age": 25
},
{
"firstName": "Lalit",
"lastName": "Kumar",
"age": 24
}
]
DW Script
%dw 2.0
input payload json
output csv header=false
---
payload
Output
firstName,lastName,age
Gaurav,Dubey,25
Lalit,Kumar,24
You can do the above example in another way.
DW Script
%dw 2.0
input payload json
output csv header=true
---
[
{
"firstName": "Gaurav",
"lastName": "Dubey",
"age": 25
},
{
"firstName": "Lalit",
"lastName": "Kumar",
"age": 24
}
]
Output
firstName,lastName,age
Gaurav,Dubey,25
Lalit,Kumar,24
References
https://docs.mulesoft.com/mule-runtime/4.3/dataweave-formats

Good one . Gaurav
Thank you.