data weave documentation

18
Data Weave Documentation (Mule soft) Presented by Sindhu VL

Upload: sindhu-vl

Post on 20-Jan-2017

338 views

Category:

Design


0 download

TRANSCRIPT

Page 1: Data weave documentation

Data Weave Documentation

(Mule soft)

Presented bySindhu VL

Page 2: Data weave documentation

What is Data Weave?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).

Page 3: Data weave documentation

Document Structure : The Header, which defines directives

(optional)The Body, which describes the output

structureThe two sections are delimited by a

separator, which is not required if no header is present. The separator consists of three dashes: "---"

Page 4: Data weave documentation

Data Weave code (.dwl)looks like : 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

} }

Page 5: Data weave documentation

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

Page 6: Data weave documentation

Sample payload(Input) to the above example : <?xml version="1.0" encoding="UTF-8"?>

<user> <name> userNameFromPayload </name><lastName>

lastNameFromPayload</lastName> </user>

Page 7: Data weave documentation

Sample output: The sample output for the example shown

above looks like : (Json format){ "user_name": “userNameFromPayload", "user_lastName": " lastNameFromPayload“ }

Page 8: Data weave documentation

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 '---'.

All directives are declared on the header section of your DataWeave document and act upon the entire scope of it. 

Directives are a mechanism to declare variables and constants and namespace aliases which need to be referenced in the Document. They are also needed to declare the type of the output of your transform. 

Page 9: Data weave documentation

Through directives you can define :DataWeave versionInput types and sourcesOutput typeNamespaces to import into your transformConstants that can be referenced throughout

the bodyFunctions that can be called throughout the

body

Page 10: Data weave documentation

Directives : 1Version Directive : Namespace Directive : Through this directive,

you specify the version of the DataWeave syntax that is used to interpret the transformation.

coded as : %dw 1.0

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.

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

Page 11: Data weave documentation

Directives : 2Input Directive : Valid input types are :Inputs are declared by

assigning a name and a content type. You can then refer to them in any part of the DataWeave body through the names defined in the directive.

Coded as : %input payload application/xml

application/jsonapplication/xmlapplication/javaapplication/csvapplication/dwtext/jsontext/xmltext/csv

Page 12: Data weave documentation

CSV Input Parsing : When defining an input of type CSV, there are a

few optional parameters you can add to the input directive to customize how the data is parsed. These are not defined in the DataWeave script but on the Mule XML code, in the Transform Message XML element.

There are two ways to achieve this : Manually add the attributes to the project’s XMLgraphical interface, by selecting the element

from the tree view in the input section and clicking the gear icon

Page 13: Data weave documentation

Directives :3Output Directive : Valid output types are

:The Output Directive

specifies what the output type is in a transformation, which is specified using content/type. Only one output can be specified, the structure of this output is then defined in the DataWeave body.

Coded as : %output application/xml

application/jsonapplication/xmlapplication/javaapplication/csvapplication/dwtext/jsontext/xmltext/csv

Page 14: Data weave documentation

Skip Null On : Whenever the output is of XML or JSON type and has null

values in its elements or attributes, you can specify whether this generates an outbound message that contains fields with "null" values, or if these fields are ignored entirely. This can be set through an attribute in the output directive named skipNullOn, which can be set to three different values: elements, attributes, or everywhere.

Like : %output application/xml skipNullOn="everywhere“

When set to: elements: A key:value pair with a null value is ignored.attributes: An XML attribute with a null value is skipped.everywhere: Apply this rule to both elements and attributes.

Page 15: Data weave documentation

DataWeave Canonical Model : DataWeave uses three basic data types: Objects,

Arrays, and Simple Types, the execution of a DataWeave transformation always produces one of these three types of data.

This expression can be built using any of the following elements:

ObjectsArraysSimple literalsVariable and Constant references

Page 16: Data weave documentation

Example Transformation to DataWeave : Input

<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from>

<heading>Reminder</heading> <body>Don't forget me this

weekend!</body></note>

Page 17: Data weave documentation

Transform : %dw 1.0 %output application/dw --- payloadOutput : { note: { to: "Tove", from: "Jani", heading: "Reminder", body: "Dont forget me this weekend!“ } }

Page 18: Data weave documentation

Still many to learn … Stay tuned!!!!!!!!!!!!