Contact Free trial Login

write

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

Writes (as a String or Binary) the given value in a specific format.

Returns a string or binary with the serialized representation of the value in the specified mimeType (format). You might use this function when you need some data in a format different from the script output, for example to embed data as JSON/CSV inside an XML.

Parameters

Name Description

value

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

contentType

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

writerProperties

Optional: Sets writer configuration properties for the particular content type. For writer configuration properties (and other content types), see DataWeave Output Formats and Writer Properties.

Example

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.

Input

[
  {
    "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"
  }
]

Source

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

Output

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

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub