Contact Us 1-800-596-4880

Transform (<ee:transform/>)

Write a DataWeave script within this component to set or transform the Mule message payload, attributes, or variables to the required output format and data structure.

DataWeave is the programming language for all expressions and data transformations in your integrations and implementations. The language offers function libraries and mechanisms for creating customizations, including your own functions, libraries, and data types. To learn the basics of DataWeave and experiment with DataWeave scripts, see DataWeave Tutorial and Playground, and find DataWeave functions and guidance with the language in the DataWeave documentation.

Component XML

This component supports the following XML structure:

<ee:transform doc:name="Transform" doc:id="xnpgke" >
    <ee:message >
        <ee:set-payload ><![CDATA[%dw 2.0
output application/java (1)
---
//DataWeave script (2)
]]></ee:set-payload>
        </ee:message>
    </ee:transform>
1 The output directive of a DataWeave script that configures the application/java format
2 Location for the body of the DataWeave script

Write a DataWeave script within a Transform component (<ee:transform/>) to create or transform content for any parts of the Mule event:

<ee:message/> and <ee:variables/> are child elements of Transform (<ee:transform/>):

  • <ee:message/> accepts child elements for creating or transforming a Mule message payload and attributes:

    Element Name Element XML Description

    Set payload

    <ee:set-payload/>

    Sets or transforms the Mule message payload. This child element of <ee:message/> accepts a DataWeave script. One <ee:set-payload/> element is allowed.

    Set attribute

    <ee:set-attributes/>

    Sets or transforms one or more Mule message attributes. This child element of <ee:message/> accepts a DataWeave script. One <ee:set-attributes/> element is allowed.

  • <ee:variables/> accepts child elements for creating or transforming a Mule variables:

    Element Name Element XML Description

    Set Variables

    <ee:set-variable/>

    Sets or transforms a Mule variable. This child element of <ee:variables/> accepts a DataWeave script. One or more <ee:set-variable/> elements are allowed.

Examples

The following examples show how to configure an inline transformation and call DataWeave scripts to apply a transformation.

Example: Inline DataWeave Transformation

The following example sets the payload using an inline DataWeave script.

<ee:transform
  doc:id="747f74"
  doc:name="Transform" >
    <ee:message>
        <ee:set-payload>
          <![CDATA[%dw 2.0
            output application/xml
            ---
            example: {
                message @(id: "DSF-829"): "Hello World!",
                date: |2017-03-21|,
                items:
                {
                    item1: 728,
                    item2: "14422"
                }
            }
          ]]>
       </ee:set-payload>
    </ee:message>
</ee:transform>

The DataWeave script within <ee:set-payload/> returns this XML output as the message’s payload:

<example>
  <message id="DSF-829">Hello World!</message>
  <date>2017-03-21</date>
  <items>
    <item1>728</item1>
    <item2>14422</item2>
  </items>
</example>

Example: Calling an External DataWeave Script

The following example calls an external DataWeave script within a DWL file to set the payload.

<ee:transform doc:id="747f74d"
  doc:name="Transform" doc:mode="immediate">
    <ee:message>
      <ee:set-payload resource="myscript.dwl" />
    </ee:message>
</ee:transform>

Add your DWL file to the src/main/resources directory of your Mule project folder. For example, you can create a file named myscript.dwl in that folder, and provide the same DataWeave script shown in Example: Inline DataWeave Transformation:

%dw 2.0
output application/xml
---
example: {
    message @(id: "DSF-829"): "Hello World!",
    date: |2017-03-21|,
    items:
    {
        item1: 728,
        item2: "14422"
    }
}

If you trigger a flow that contains the Transform example, the script returns the same payload returned by Example: Inline DataWeave Transformation.

Example: Payload, Attribute, and Variable Transformation

The following example triggers a flow that requests user data and configures <ee:transform/> to transform the user data payload, select an attribute that provides the status code of the HTTP request, and set the value of Mule variables. The loggers in the example help track the values within the Mule event in the flow.

<http:listener-config name="HTTP_Listener_config" >
  <http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" >
  <http:request-connection port="443" />
</http:request-config>

<flow name="myFlow" >
  <http:listener path="/mytrigger" config-ref="HTTP_Listener_config"
    doc:name="Listener" doc:id="kiohjs" /> (1)
  <http:request method="GET" doc:name="Request"
     config-ref="HTTP_Request_configuration"
     url="https://jsonplaceholder.typicode.com/users"/> (2)
  <set-variable variableName="myVar" value="myVar Value"
    doc:name="Set variable" doc:id="tgwhvc" /> (3)
  <logger level="INFO" message="#[payload[0]]" doc:name="Logger Payload"
    doc:id="9dbeda-430b1f" category="LOGGER-PAYLOAD-1"/> (4)
  <logger level="INFO" message="#[attributes]" doc:name="Logger Attributes"
    doc:id="9dbeda-430b1g" category="LOGGER-ATTRIBUTES-1"/> (5)
  <logger level="INFO" message="#[vars]" doc:name="Logger Variables"
    doc:id="9dbeda-430b1h" category="LOGGER-VARIABLES-1"/> (6)
  <ee:transform doc:name="Transform" doc:id="qtxpfa" > (7)
      <ee:message>
          <ee:set-payload>
            <![CDATA[%dw 2.0
                output application/json
                ---
                payload.id
            ]]>
          </ee:set-payload>
          <ee:set-attributes>
            <![CDATA[%dw 2.0
            output application/json
            ---
            {
                'status-code' : attributes.statusCode
            }]]>
          </ee:set-attributes>
      </ee:message>
      <ee:variables>
          <ee:set-variable variableName="myVar2">
              <![CDATA[%dw 2.0
                  output application/json
                  ---
                  vars.myVar
              ]]>
          </ee:set-variable>
          <ee:set-variable variableName="myVar3">
              <![CDATA[%dw 2.0
              output application/json
              ---
              {
                  'myNewVar' : 'myvariable3'
              }]]>
          </ee:set-variable>
      </ee:variables>
  </ee:transform>
  <logger level="INFO" message="#[payload]" doc:name="Logger Payload 2"
          doc:id="9dbeda-430b1c" category="LOGGER-PAYLOAD-2"/> (8)
  <logger level="INFO" message="#[attributes]" doc:name="Logger Attributes 2"
          doc:id="9dbeda-430b1d" category="LOGGER-ATTRIBUTES-2"/> (9)
  <logger level="INFO" message="#[vars.myVar2]" doc:name="Logger Variables 2"
          doc:id="9dbeda-430b1e" category="LOGGER-VARIABLES-2"/> (10)
  <logger level="INFO" message="#[vars.myVar3.myNewVar]"
          doc:name="Logger Variables 3"
          doc:id="9dbeda-430b1g" category="LOGGER-VARIABLES-3"/> (11)
</flow>
1 <http:listener/> configures an HTTP Listener operation that triggers the flow from requests to localhost port 8081 at the endpoint /mytrigger.
2 <http:request/> configures and HTTP Request operation for a JSON data sample.
3 <set-variable/> sets Mule variable myVar to the string value "myVar Value".
4 LOGGER-PAYLOAD-1 prints the value of payload[0], which is the first index of an array of user objects found at https://jsonplaceholder.typicode.com/users.
5 LOGGER-ATTRIBUTES-1 is set to the predefined Mule variable attributes to print the HTTP metadata returned from the HTTP request, including the status code, reason phrase, and headers.

For more information about attributes and other Mule variables, see Predefined Variables.

6 LOGGER-VARIABLES-1 is set to vars to print an object that describes the Mule variable in the Mule event:
{myVar=TypedValue[value: 'my VarValue',
  dataType: 'SimpleDataType{type=java.lang.String,
  mimeType='*/*; charset=UTF-8'}']}
7 <ee:transform/> provides DataWeave scripts that select and transform payload, attribute, and variable data in the Mule event.
  • <ee:set-payload/> selects the id value from the user objects in the payload array.

  • <ee:set-attributes/> uses attributes.statusCode to select the status code value from the HTTP request metadata.

    All the metadata is selectable. For example, you can select the reason phrase with attributes.reasonPhrase and the headers with attributes.headers. For more information, see DataWeave Selectors.

  • <ee:variables/> creates new Mule variables. The first <ee:set-variable/> sets myVar2 to the value of the myVar variable (vars.myVar). The second creates a variable named myVar3 to the string 'myvariable3'.

8 LOGGER-PAYLOAD-2 prints the transformed value of payload, which is now an array of user id values ([1,2,3,4,5,6,7,8,9,10]).
9 LOGGER-ATTRIBUTES-2 prints the transformed value of attributes, which is now the JSON object { "status-code": 200 }.
10 LOGGER-VARIABLES-2 prints the value of Mule variable vars.myVar2, which is the string "myVar Value".
11 LOGGER-VARIABLES-3 prints the value of Mule variable vars.myVar3.myNewVar, which is the string "myvariable3".

In the logs, the output looks something like this (edited for readability):

INFO  2023-10-24 16:59:43,279 ...
  [processor: myFlow/processors/2; event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-PAYLOAD-1: {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  }
INFO  2023-10-24 16:59:43,333 ...
  [processor: myFlow/processors/3; event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-ATTRIBUTES-1: org.mule.extension.http.api.HttpResponseAttributes
    {
       Status Code=200
       Reason Phrase=OK
       Headers=[
          date=Wed, 25 Oct 2023 20:00:48 GMT
          content-type=application/json; charset=utf-8
          transfer-encoding=chunked
          connection=keep-alive
          x-powered-by=Express
          x-ratelimit-limit=1000
          x-ratelimit-remaining=999
          x-ratelimit-reset=1698156189
          vary=Origin, Accept-Encoding
          access-control-allow-credentials=true
          cache-control=max-age=43200
          pragma=no-cache
          expires=-1
          x-content-type-options=nosniff
          etag=W/"160d-1eMSsxeJRfnVLRBmYJSbCiJZ1qQ"
          via=1.1 vegur
          cf-cache-status=HIT
          age=21484
          report-to={"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=abcdefg1234..."}],"group":"cf-nel","max_age":604800}
          nel={"success_fraction":0,"report_to":"cf-nel","max_age":604800}
          server=cloudflare
          cf-ray=81bd0c408c729685-SJC
          alt-svc=h3=":443"; ma=86400
       ]
    }
INFO  2023-10-24 16:59:43,347 ... [processor: myFlow/processors/4;
  event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-VARIABLES-1: {myVar=TypedValue[value: 'myVar Value',
    dataType: 'SimpleDataType{type=java.lang.String, mimeType='*/*; charset=UTF-8'}']}
INFO  2023-10-24 16:59:43,512 ... [processor: myFlow/processors/6;
  event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-PAYLOAD-2: [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10
  ]
INFO  2023-10-24 16:59:43,519 [processor: myFlow/processors/7;
  event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-ATTRIBUTES-2: {
    "status-code": 200
  }
INFO  2023-10-24 16:59:43,528 ... [processor: myFlow/processors/8;
  event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-VARIABLES-2: "myVar Value"
INFO  2023-10-24 16:59:43,576 ... [processor: myFlow/processors/9;
  event: 6699ea20-72c9-11ee-b3b5-147ddaaf4f97]
  LOGGER-VARIABLES-3: "myvariable3"