Transforming Data

DataWeave is a language that meets the requirements of flexible data integration. It lets you query and transform data and thereby enables efficient application integration. The process of converting data or information into a target format is called data transformation. Today’s enterprise infrastructures rarely use the same data formats. Transforming data structures is therefore a central topic in enterprise application integration. DataWeave supports the common data formats JSON, XML, Java, CSV, Excel, COBOL copybook, and flat files.

DataWeave 1.0

A DataWeave script consists of a header and a body, separated by ---.

DataWeave Header

The header allows the following elements on separate lines, each preceded by a %.

ElementDescriptionExample
%dwDataWeave version%dw 1.0
%outputOutput format as a MIME type%output application/json
%inputInput format as a MIME type%input application/xml
%namespaceXML namespaces%namespace soap http://www.w3.org/2003/05/soap-envelope
%varConstants that can be used later in the header or in the script body. Reference expressions of the DataWeave Expression Language are supported.%var user = {"firstname": "John", "lastname": "Doe", "user_id" : "jdoe"}
%functionFunction. The DataWeave Expression Language is supported.%function fullname(user) (user.firstname ++ ' ' ++ user.lastname)
%typeCustom data type%type order = :object {class: "com.dataliquid.domain.User"}

DataWeave Body

The DataWeave body describes the transformation logic and defines the output structure. The range of applications is broad, from simple mappings to complex transformation rules. Support for data types and an extensive set of functions (merging, aggregation, enrichment, filtering) makes it easier to implement transformation rules with complex business logic.

Input

{
  "fname": "John",
  "name": "Doe",
  "id" : "jdoe"
}

DataWeave script

weave
%dw 1.0
%output application/json
---
{
  uid : payload.id,
  firstname : payload.fname,
  lastname : payload.name
}

Output

json
{
  "uid": "jdoe",
  "firstname": "John",
  "lastname": "Doe"
}

Summary

This article introduced the fundamentals of DataWeave. Beyond comparatively simple data mapping, DataWeave can perform very complex transformations. As an important component of Mule ESB, DataWeave simplifies data integration considerably.

Share LinkedIn X Email