DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

write(Any, String, Object): String | Binary

Writes a value as a string or binary in a supported format.

Returns a string or binary with the serialized representation of the value in the specified format (MIME type). This function can write to a different format than the input. Note that the data must validate in that new format, or an error will occur. For example, application/xml content is not valid within an application/json format, but text/plain can be valid.


Name Description


The value to write. The value can be of any supported data type.


A supported format (or MIME type) to write. Default: application/dw.


Optional: Sets writer configuration properties. For writer configuration properties (and other supported MIME types), see DataWeave Output Formats and Writer Properties.


This example writes the string world in plain text (text/plain"). It outputs that string as the value of a JSON object with the key hello.


%dw 2.0
output application/json
{ hello : write("world", "text/plain") }


{ "hello": "world" }


This example takes JSON input and writes the payload to a CSV format that uses a pipe (|) separator and includes the header (matching keys in the JSON objects). Note that if you instead use "header":false in your script, the output will lack the Name|Email|Id|Title header in the output.


%dw 2.0
output application/xml
{ "output" : write(payload, "application/csv", {"header":true, "separator" : "|"}) }


    "Name": "Mr White",
    "Email": "white@mulesoft.com",
    "Id": "1234",
    "Title": "Chief Java Prophet"
    "Name": "Mr Orange",
    "Email": "orange@mulesoft.com",
    "Id": "4567",
    "Title": "Integration Ninja"


<?xml version="1.0" encoding="US-ASCII"?>
Mr White|white@mulesoft.com|1234|Chief Java Prophet
Mr Orange|orange@mulesoft.com|4567|Integration Ninja

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!