Using the DataWeave Transformer
The DataWeave Text Editor UI
The Graphical UI
Defining Input Structure
Explicitly Defining a MIME Type
DataWeave is Mule’s most powerful and versatile tool for transforming
data. TheTransform Message component carries out a transformation
of your Mule message that follows a transform script, this transform
script can be explicitly written in DataWeave code or you can use the
UI to build it implicitly through dragging and dropping elements.
DataWeave fully supports DataSense, allowing you to leverage
metadata from connectors, schemas and sample documents to more
easily design your transformations. DataSense provides content assist
while you are coding and scaffolds and auto generates lines of code
form actions performed in the UI. TheTransform Message component
offers you a preview of your output that is built on sample data and is
updated in real time as you make changes to your transform, so that
you can be sure of what you’ll be getting out of the other end.
USING THE DATAWEAVE
When adding a Transform Message element to a Mule Flow, it
takes the elements from the incoming Mule Message as its
inputs. It then performs the necessary actions to produce a
Mule message as output for the next element in the flow.
THE DATAWEAVE TEXT EDITOR UI
Although DataWeave as a language supports adding input
directives and naming these by any name you like, when using
DataWeave in Anypoint Studio, it’s not necessary to declare any
input directives for any of the components of the Mule
Message that arrives to the DataWeave transformer (Payload,
flow variables and input/outbound properties) nor for any
system variables. These are already implicitly recognized as
inputs and can be referenced anywhere in the DataWeave body
without a need to include them in the header, their type is
known from Mule metadata.
THE GRAPHICAL UI
Two tree views show the known metadata contents of the incoming
and the outgoing Mule Messages, allowing you to explore them and
know what data is available for using as an input and where it can
fit into, and how to reference each of these parts.
You can simplify or hide the graphical UI if you wish by clicking the
icons on the top right to select between the different views for this
DEFINING INPUT STRUCTURE
If the other elements in your Mule flow expose metadata about their
input and output, then this information will already be available to
the Transform Message component. If they don’t, you can configure
these elements so that they expose this information by editing
their Metadata tab. For example, you can configure an HTTP
connector and assign it a JSON sample file so that this sample’s
structure is exposed as metadata that your Transform Message
component can read.
If the Mule flow doesn’t expose Metadata about the elements you
need from the incoming message, you can also manually specify it
directly in the input section of your Tranform Message component. If
a metadata definition is missing, a notification will advise you to
provide one, otherwise you can also right-click your input and
select Set Metadata.
YOU CAN SELECT AN EXISTING METADATA TYPE, THIS MAY SAVE YOU
OR YOU CAN ALSO BUILD YOUR METADATA FREELY USING AN EDITOR:
This sample data is used together with your DataWeave code to
produce a sample output in the output section, which gets updated in
real time as you make changes.
You can then define the data structure manually by writing or pasting a
sample into the newly created tab.
By default, DataWeave should be able to recognize the type of an input
metadata. If you must explicitly define an input payload type, use the
attribute in an XML tag as in the example below .
Explicitly Defining a MIME Type
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).