---
schema_version: "1"
id: 6a4aceab921a1681374d2b757a31a4e732e7cb1ce58a23e1d669550400ac40a2
title: JSON Format
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-formats-json.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-formats-json
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 5e4e21875e02d267f7ae79d25942e808fba2a4befa07b6145e19462cf59e2d62
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
keywords: format, json, */json, */*+json, application/json
---

# JSON Format

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

MIME type: `application/json`

ID: `json`

In the JSON data format, values map one-to-one with DataWeave values. JSON supports `String`,
`Boolean`, `Number`, `Null`, `Object`, and `Array` types. DataWeave supports each of these values
natively.

The DataWeave reader for JSON input supports the following parsing strategies:

- Indexed

- In-Memory

- Streaming

To understand the parsing strategies that DataWeave readers and writers can apply to this format,
see [DataWeave Parsing Strategies](https://docs.mulesoft.com/dataweave/2.9/dataweave-formats#dw_readers_writers).

## Examples

The following examples show uses of the JSON format:

- [Example: Represent JSON in the DataWeave (dw) Format](#example1)

- [Example: Convert Repeated XML Elements to JSON](#example2)

- [Example: Stream JSON Data](#example3)

### Example: Represent JSON in the DataWeave (dw) Format

This example shows how JSON input is represented in the DataWeave (dw) format.

#### Input

The following JSON object serves as the input payload to the DataWeave source.

```json
{
  "name": "Matias",
  "age": 8,
  "male": true,
  "kids": null,
  "brothers": ["Pedro", "Sara"]
}
```

#### Source

The DataWeave script transforms the JSON input payload to the DataWeave (dw) format and MIME type.

```dataweave
output application/dw
---
payload
```

#### Output

Notice that only the keys of the JSON input and the `application/dw` output differ. The JSON keys
are surrounded by quotation marks. These DataWeave (dw) keys do not require quotation marks. See
[Valid Keys](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-extract-data#valid-keys) for details.

```dataweave
{
  name: "Matias",
  age: 8,
  male: true,
  kids: null,
  brothers: [
    "Pedro",
    "Sara"
  ]
}
```

### Example: Convert Repeated XML Elements to JSON

This example shows how to convert repeated XML elements to JSON.

Note that XML encodes collections using repeated (unbounded) elements. DataWeave represents
unbounded elements by repeating the same key.

#### Input

The following XML data contains repeating child elements with the key `name`. This XML serves as the
input payload to the DataWeave source.

```xml
<?xml version='1.0' encoding='UTF-8'?>
<friends>
  <name>Mariano</name>
  <name>Shoki</name>
  <name>Tomo</name>
  <name>Ana</name>
</friends>
```

#### Source

The following DataWeave script takes the XML input as its payload and outputs that payload in the
JSON format.

The script selects the `name` elements from the XML input and uses the DataWeave `map` function to
map the values of those elements into an array of objects. Each object in the array takes the form
`{ name : item }`, where `item` is the value of the `name` element. The entire array serves as the
value of the `friends` key.

```dataweave
%dw 2.0
output application/json
---
friends: payload.friends.*name map (
            (item, index) -> {name: item}
         )
```

#### Output

The DataWeave script outputs the following JSON object.

```json
{
  "friends": [
    {
      "name": "Mariano"
    },
    {
      "name": "Shoki"
    },
    {
      "name": "Tomo"
    },
    {
      "name": "Ana"
    }
  ]
}
```

### Example: Stream JSON Data

To demonstrate streaming, the following example streams a JSON file by reading each element in an
array one at a time.

#### Input

The JSON input payload serves as input for the XML configuration.

JSON Input (truncated):

```json
{
  "myJsonExample": [
    {
      "name": "Shoki",
      "zipcode": "95838"
    },
    {
      "name": "Leandro",
      "zipcode": "95823"
    },
    {
      "name": "Mariano",
      "zipcode": "95815"
    },
    {
      "name": "Cristian",
      "zipcode": "95815"
    },
    {
      "name": "Kevin",
      "zipcode": "95824"
    },
    {
      "name": "Stanley",
      "zipcode": "95841"
    }
  ]
}
```

#### XML Configuration

The following XML configuration streams JSON input.

```xml
<file:config name="File_Config" doc:name="File Config" />
<flow name="dw-streaming-jsonFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="1" timeUnit="MINUTES"/>
    </scheduling-strategy>
  </scheduler>
  <file:read doc:name="Read"
     config-ref="File_Config"
     path="${app.home}/myjsonarray.json"
     outputMimeType="application/json; streaming=true"/>
  <ee:transform doc:name="Transform Message" >
    <ee:message >
      <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload.myJsonExample map ((element) -> {
returnedElement : element.zipcode
})]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <file:write doc:name="Write"
    path="/path/to/output/file/output.json"
    config-ref="File_Config1"/>
  <logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
```

- The streaming example configures the File Read operation to stream the JSON input by setting
  `outputMimeType="application/json; streaming=true"`. In the Studio UI, you can set the **MIME
  Type** on the listener to `application/json` and the **Parameters** for the MIME Type to **Key**
  `streaming` and **Value** `true`.

- The DataWeave script in the **Transform Message** component iterates over the array in the input
  payload and selects its `zipcode` values.

- The Write operation returns a file, `output.json`, which contains the result of the
  transformation.

- The Logger prints the same output payload that you see in `output.json`.

#### Output

The JSON streaming example produces a JSON array of objects.

```json
[
  {
    "returnedElement": "95838"
  },
  {
    "returnedElement": "95823"
  },
  {
    "returnedElement": "95815"
  },
  {
    "returnedElement": "95815"
  },
  {
    "returnedElement": "95824"
  },
  {
    "returnedElement": "95841"
  }
]
```

## Configuration Properties

DataWeave supports the following configuration properties for this format.

### Reader Properties

This format accepts properties that provide instructions for reading input data.

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td><p><code>streaming</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Streams input when set to <code>true</code>. Use only if entries are accessed sequentially. The input must be a top-level array. See the <a href="#example2">streaming example</a>, and see <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-formats#dw_readers_writers">DataWeave Readers</a>.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr></tbody></table>

### Writer Properties

This format accepts properties that provide instructions for writing output data.

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td><p><code>bufferSize</code></p></td><td><p><code>Number</code></p></td><td><p><code>8192</code></p></td><td><div><div><p>Size of the buffer writer, in bytes. The value must be greater than <code>8</code>.</p></div></div></td></tr><tr><td><p><code>deferred</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Generates the output as a data stream when set to <code>true</code>, and defers the script’s execution until the generated content is consumed.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr><tr><td><p><code>duplicateKeyAsArray</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Converts the values of duplicate keys in an object to a single array of values to the duplicated key.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr><tr><td><p><code>encoding</code></p></td><td><p><code>String</code></p></td><td><p><code>'UTF-8'</code></p></td><td><div><div><p>The encoding to use for the output, such as UTF-8.</p></div></div></td></tr><tr><td><p><code>indent</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>true</code></p></td><td><div><div><p>Write indented output for better readability by default, or compress output into a single line when set to <code>false</code>.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr><tr><td><p><code>skipNullOn</code></p></td><td><p><code>String</code></p></td><td><p><code>null</code></p></td><td><div><div><p>Skips <code>null</code> values in the specified data structure. By default, DataWeave does not skip the values.</p></div><div><ul><li><p><code>arrays</code> + Ignore and omit <code>null</code> values inside arrays from the JSON output, for example, with <code>output application/json skipNullOn="arrays"</code>.</p></li><li><p><code>objects</code> + Ignore key-value pairs that have <code>null</code> as the value, for example, with <code>output application/json skipNullOn="objects"</code>.</p></li><li><p><code>everywhere</code> + Apply <code>skipNullOn</code> to arrays and objects, for example, <code>output application/json skipNullOn="everywhere"</code>.</p></li></ul></div><div><p>Valid values are <code>arrays</code> or <code>objects</code> or <code>everywhere</code>.</p></div></div></td></tr><tr><td><p><code>writeAttributes</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Converts attributes of a key into child key-value pairs of that key. The attribute key name starts with @.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr></tbody></table>

## Supported MIME Types

This format supports the following MIME types.

<table><colgroup><col></colgroup><thead><tr><th>MIME Type</th></tr></thead><tbody><tr><td><p><code>*/json</code></p></td></tr><tr><td><p><code><strong>/</strong>+json</code></p></td></tr></tbody></table>
