%dw 2.0
output application/json indent = false
---
{
hello : "world",
bello : "world",
mello : "world"
}
Set Reader and Writer Configuration Properties
DataWeave provides configuration properties for data formats, such as JSON (application/json
), XML (application/xml
), and (application/csv
). The properties change the behavior of DataWeave readers and writers for those formats. For example, the default separator for a CSV reader is a comma (,
). You can use the format’s separator
property to specify a different separator for CSV content.
Refer to DataWeave Formats for more details on available reader and writer properties for various data formats.
Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For DataWeave in Mule 3 apps, refer to DataWeave version 1.2 examples. For other DataWeave versions, you can use the version selector in the DataWeave table of contents.
Use a Writer Property in an Output Directive
The following example shows how to append writer properties to the DataWeave output
directive. The script uses indent = false
to compress the JSON output into a single line.
{"hello": "world","bello": "world","mello": "world"}
The following examples also append writer configuration properties to the output
directive:
-
Avro example that uses
schemaUrl
-
Excel (XLSX) example that uses
header=true
-
Flat File example that sets a
schemaPath
-
XML example that uses
inlineCloseOn="empty"
to close any empty XML elements
Use Reader and Writer Properties in DataWeave Functions
The DataWeave read
, readUrl
, and write
functions accept one or more comma-separated property configurations within curly braces.
In the header of the following script, the value of myVar
is a read
function that inputs an XML sample with an empty child element (<ex1></ex1>
). The function passes an XML reader property {nullValueOn: "empty"}
that converts the value of the empty element to null
.
In the body of the script, a write
function accepts the value of myVar
as input. The function passes the JSON writer properties {skipNullOn:"objects", writeAttributes:true}
to skip the object with the null
value (<ex1>null</ex1>
) and to write the attribute and value of <ex3 a='greeting'>hello</ex3>
.
%dw 2.0
var myVar = read("<greeting><ex1></ex1><ex2>hello</ex2><ex3 a='greeting'>hello</ex3></greeting>", "application/xml", {nullValueOn: "empty"})
output application/json with binary
---
write(myVar.greeting, "application/json", {skipNullOn:"objects", writeAttributes:true})
{
"ex2": "hello",
"ex3": {
"@a": "greeting",
"__text": "hello"
}
}
The following examples pass reader properties to readUrl
: