Contact Us 1-800-596-4880

JSON Format

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.

Examples

The following examples show uses of the JSON format:

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.

{
  "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.

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 for details.

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

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

Output

The DataWeave script outputs the following JSON object.

{
  "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):
{ "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.

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

[
  {
    "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.

Parameter Type Default Description

streaming

Boolean

false

Streams input when set to true. Use only if entries are accessed sequentially. The input must be a top-level array. See the streaming example, and see DataWeave Readers.

Valid values are true or false.

Writer Properties

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

Parameter Type Default Description

bufferSize

Number

8192

Size of the buffer writer, in bytes. The value must be greater than 8.

deferred

Boolean

false

Generates the output as a data stream when set to true, and defers the script’s execution until the generated content is consumed.

Valid values are true or false.

duplicateKeyAsArray

Boolean

false

Converts the values of duplicate keys in an object to a single array of values to the duplicated key.

Valid values are true or false.

encoding

String

'UTF-8'

The encoding to use for the output, such as UTF-8.

indent

Boolean

true

Write indented output for better readability by default, or compress output into a single line when set to false.

Valid values are true or false.

skipNullOn

String

null

Skips null values in the specified data structure. By default, DataWeave does not skip the values.

  • arrays + Ignore and omit null values inside arrays from the JSON output, for example, with output application/json skipNullOn="arrays".

  • objects + Ignore key-value pairs that have null as the value, for example, with output application/json skipNullOn="objects".

  • everywhere + Apply skipNullOn to arrays and objects, for example, output application/json skipNullOn="everywhere".

Valid values are arrays or objects or everywhere.

writeAttributes

Boolean

false

Converts attributes of a key into child key-value pairs of that key. The attribute key name starts with @.

Valid values are true or false.

Supported MIME Types

This format supports the following MIME types.

MIME Type

*/json

/+json