data weave documentation
TRANSCRIPT
Data Weave Documentation
(Mule soft)
Presented bySindhu VL
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).
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: "---"
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
} }
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 payload(Input) to the above example : <?xml version="1.0" encoding="UTF-8"?>
<user> <name> userNameFromPayload </name><lastName>
lastNameFromPayload</lastName> </user>
Sample output: The sample output for the example shown
above looks like : (Json format){ "user_name": “userNameFromPayload", "user_lastName": " lastNameFromPayload“ }
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.
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
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
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
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
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
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.
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
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>
Transform : %dw 1.0 %output application/dw --- payloadOutput : { note: { to: "Tove", from: "Jani", heading: "Reminder", body: "Dont forget me this weekend!“ } }
Still many to learn … Stay tuned!!!!!!!!!!!!