Over a million developers have joined DZone.

How to Start Using MuleSoft's DataWeave

DataWeave is the type of transformation provided by MuleSoft, which is built on top of Data Mapper. It's very easy to learn and makes developers' lives easy.

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

DataWeave is the type of transformation provided by MuleSoft, which is built on top of Data Mapper. It's very robust in nature and can transform irrespective of mapping complexity (Simple mappings, medium-complex mappings, and complex mappings).

It's very easy to learn and makes developers' lives easy :)

DataWeave provides real-time actionable insights by collecting, curating, and analyzing data from multiple sources at a very large scale, across geographies. The Data Weave Language is a powerful template engine that allows you to transform data to and from any kind of format (XML, CSV, JSON, POJOs, Maps, etc.).

Document Structure

Here is the structure to be followed while developing the DataWeave code.

  • The Header, which defines directives (optional).

  • The Body, which describes the output structure.

  • The two sections are delimited by a separator, which is not required if no header is present. The separator consists of three dashes: "---".

Data Weave code (.dwl) looks like: (A very basic sample code)

  • This code describes a conversion from a JSON input to an XML output:

%dw 1.0 
%input application/json 
 %output application/xml 
 user: {
 name: payload.user_name,
 lastName: payload.user_lastName 
  • If Output is other than JSON , then just replace JSON to required form, like XML, CSV etc. in the header part:

%input application/json

• Payload attribute is the input data and each field should be separated by comma:

 name: payload.user_name,
 lastName: payload.user_lastName 
  • Sample Input : (XML)

<?xml version="1.0" encoding="UTF-8"?> <user>
 <name> userNameFromPayload </name>
<lastName> lastNameFromPayload</lastName>
  • Sample Output: (JSON)

 "user_name": “userNameFromPayload",
 "user_lastName": "lastNameFromPayload"

Through directives you can define:

  • DataWeave version

  • Input types and sources

  • Output type

  • Namespaces to import into your transform

  • Constants that can be referenced throughout the body

  • Functions that can be called throughout the body

Data Weave Canonical Model

DataWeave uses three basic data types: Objects, Arrays, and Simple Types, the execution of a DataWeave transformational ways produces one of these three types of data. This expression can be built using any of the following elements:

  • Objects

  • Arrays

  • Simple literals

  • Variable and constant references

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

java 1.7,mulesoft,anypoint studio,mule 3.7

Published at DZone with permission of sindhu vankam. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}