+
+

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.

DataWeave Script:
%dw 2.0
output application/json indent = false
---
{
	hello : "world",
	bello : "world",
	mello : "world"
}
Output JSON:
{"hello": "world","bello": "world","mello": "world"}

The following examples also append writer configuration properties to the output directive:

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

DataWeave Script:
%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})
Output JSON:
{
  "ex2": "hello",
  "ex3": {
    "@a": "greeting",
    "__text": "hello"
  }
}

The following examples pass reader properties to readUrl:

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!