What is DataWeave?

Reading Time: 3 minutes

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.

NameMIME TypeIDSupported Formats
AVROapplication/avroavroAvro Format
CSVapplication/csvcsvCSV Format
DWapplication/dwdwDataWeave Format
FLATFILEapplication/flatfileflatfileFlat File Format, COBOL Copybook Format, Fixed Width Format
JAVAapplication/javajavaJava Format, Enum Format for Java
JSONapplication/jsonjsonJSON Format
BINARYapplication/octet-streambinaryBinary Format
EXCELapplication/xlsxexcelExcel Format
XMLapplication/xmlxmlXML Format, CData Custom Type
NDJSONapplication/x-ndjsonndjsonNewline Delimited JSON Format (ndjson)
UNLENCODEDapplication/x-www-form-urlencodedurlencodedURL Encoded Format
YAMLapplication/yamlyamlYAML Format
MULTIPARTmultipart/form-datamultipartMultipart Format
TEXTtext/plaintextText Plain Format
PROPERTIEStext/x-java-propertiespropertiesText 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

Written by 

Gaurav Dubey is a Java Developer at Knoldus Software LLP. He has done M.CA from Kurukshetra University and completed Bachelor's Degree in Computer Science from M. D. University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in Java practice. On the personal front, he loves to cook and travel.

2 thoughts on “What is DataWeave?3 min read

Leave a Reply

%d bloggers like this: