Data weave reference documentation

9
DataWeave Reference Documentation

Transcript of Data weave reference documentation

Page 1: Data weave reference documentation

DataWeave Reference Documentation

Page 2: Data weave reference documentation

2

DataWeave Reference Documentation

The DataWeave 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).

Let us start off with some examples to demonstrate the prowess of

Dataweave as a data transformation tool.

Page 3: Data weave reference documentation

3

Basic Example

This example shows a simple mapping from JSON to XML

Input

Json{ "title": "Java 8 in Action: Lambdas, Streams, and functional-style programming", "author": "Mario Fusco", "year": 2014}

Page 4: Data weave reference documentation

4

Basic Example

Transform:

%dw 1.0%output application/xml{ order: { type: "Book", title: payload.title, details: "By $(payload.author) ($(payload.year))" }}Output:<?xml version='1.0' encoding='UTF-8'?><order> <type>Book</type> <title>Java 8 in Action: Lambdas, Streams, and functional-style programming</title> <details>By Mario Fusco (2014)</details></order>

Page 5: Data weave reference documentation

5

Providing an External HTTP or HTTPS Port

DataWeave files are divided into two main sections:

1. .The Header, which defines directives (optional)

2. .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: "---"

Page 6: Data weave reference documentation

6

Providing an External HTTP or HTTPS Port

Header

The DataWeave header contains the directives, which define high level information about your transformation. The structure of the Header is a sequence of lines, each with its own Directives. The Header is terminated with '---'.

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

Page 7: Data weave reference documentation

7

Providing an External HTTP or HTTPS Port

Version Directive

Through this directive, you specify the version of the DataWeave syntax that is used to interpret the transformation.

DataWeave%dw 1.0

Namespace Directive

This directive associates an alias with its subsequent URI. The directive is relevant only when either the input or the output is of type XML.

DataWeave %namespace mes http://www.mulesoft.com/anypoint/SOA/message/v1.0

Page 8: Data weave reference documentation

8

Providing an External HTTP or HTTPS Port

Inputs are declared by assigning a name and a content type. You may define as many input directives as you want. You can then refer to them (or their child elements) in any part of the DataWeave body through the names defined in the directive.

DataWeave%input payload application/xml

Valid types are:

•application/json•application/xml•application/java•application/csv•application/dw•text/json•text/xml•text/csv

Page 9: Data weave reference documentation