---
schema_version: "1"
id: 84873a96185ac3fe4ac55a15b1cf09cb85afe026b972336d9aa8468059635d88
title: write
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dw-core-functions-write.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-write
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b8ae4a4e3a659ad61b6e8a74ca228b8ae52428baca047c34c3817ef445f6c799
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
---

# write

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## write(value: Any, contentType: String = "application/dw", writerProperties: 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. It returns a `String`
value for all text-based data formats (such as XML, JSON , CSV) and a `Binary` value for all the
binary formats (such as Excel, MultiPart, OctetStream).

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The value to write. The value can be of any supported data type.</p></td></tr><tr><td><p><code>contentType</code></p></td><td><p>A supported format (or MIME type) to write. Default: <code>application/dw</code>.</p></td></tr><tr><td><p><code>writerProperties</code></p></td><td><p>Optional: Sets writer configuration properties. For writer configuration properties (and other supported MIME types), see <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats">Supported Data Formats</a>.</p></td></tr></tbody></table>

### Example

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`.

#### Source

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

#### Output

```JSON
{ "hello": "world" }
```

### 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.

#### Source

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

#### Input

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

#### Output

```XML
<?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>
```
