Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Start Using MuleSoft's DataWeave

DZone's Guide to

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 ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

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>
 </user>
  • 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

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
java 1.7 ,mulesoft ,anypoint studio ,mule 3.7

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}