{
"name": "Matias",
"age": 8,
"male": true,
"kids": null,
"brothers": ["Pedro", "Sara"]
}
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.
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}
)
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.
{ "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 toapplication/json
and the Parameters for the MIME Type to Keystreaming
and Valuetrue
. -
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
.
Configuration Properties
DataWeave supports the following configuration properties for JSON.
Reader Properties (for JSON)
The JSON format accepts properties that provide instructions for reading input data.
Parameter | Type | Default | Description |
---|---|---|---|
|
|
|
Property for streaming input. Use only if
entries are accessed sequentially. Valid values are |
Writer Properties (for JSON)
The JSON format accepts properties that provide instructions for writing output data.
Parameter | Type | Default | Description |
---|---|---|---|
|
|
|
Size of the writer buffer. |
|
|
|
When set to |
|
|
|
If duplicate keys are detected
in an object, the writer changes the value to an array with all those values.
Valid values are |
|
|
|
The character set to use for the output. |
|
|
|
Indicates whether to indent the JSON code for
better readability or to compress the JSON into a single line.
Valid values are |
|
|
|
Skips
|
|
|
|
Indicates whether to add attributes of a key to children of the key. The new attribute key name will start with @. Valid options are |