In some cases, it is necessary to modify or specify aspects of the format
through format-specific properties. For example, you can specify CSV input and
output properties, such as the separator (or delimiter) to use in the CSV file.
For Cobol copybook, you need to specify the path to a schema file using the
schemaPath property.
You can append reader properties to the MIME type (outputMimeType) attribute
for certain components in your Mule app. Listeners and Read operations accept
these settings. For example, this On New File listener example identifies the , separator for a CSV input file:
Example: Properties for the CSV Reader
<file:listener doc:name="On New File" config-ref="File_Config" outputMimeType='application/csv; separator=","'>
<scheduling-strategy >
<fixed-frequency frequency="45" timeUnit="SECONDS"/>
</scheduling-strategy>
<file:matcher filenamePattern="comma_separated.csv" />
</file:listener>
Note that the outputMimeType setting above helps the CSV reader interpret
the format and delimiter of the input comma_separated.csv file, not the writer.
To specify the output format, you can provide the MIME type and any writer
properties for the writer, such as the CSV or JSON writer used by a File Write
operation. For example, you might need to write a pipe (|) delimiter in your
CSV output payload, instead of some other delimiter used in the input. To do
this, you append the property and its value to the output directive of a
DataWeave expression. For example, this Write operation specifies the pipe as
a separator:
Example: output Directive for the CSV Writer
<file:write doc:name="Write" config-ref="File_Config" path="my_transform">
<file:content ><![CDATA[#[output application/csv separator="|" --- payload]]]></file:content>
</file:write>
The following example shows a DataWeave script in which the output directive specifies two writer properties, one for the CSV field separator, another to indicate that there is no header.
%dw 2.0
output application/csv separator=";", header=false
---
payload
Writer properties depend on the format. The following example uses the JSON writer property, skipNullOn:
%dw 2.0
output application/json skipNullOn="everywhere"
---
payload
The following example shows a DataWeave script that uses an invisible ASCII character as a separator for the CSV output. The separator property value is \u001E. \u is the escape sequence representation and 001E is the hexadecimal representation of the record separator (RS) ASCII character.
%dw 2.0
output application/csv separator = "\u001E"
---
[
{
"Name" : "Tom",
"UID" : 2569,
"ShipOrderID" : "ui-288188"
},
{
"Name" : "Alan",
"UID" : 7756,
"ShipOrderID" : "xj-232142"
},
{
"Name" : "Dan",
"UID" : 7821,
"ShipOrderID" : "uk-259459"
}
]
The CSV example produces the following output:
NameUIDShipOrderID
Tom2569ui-288188
Alan7756xj-232142
Dan7821uk-259459
Scripts that use the read or readUrl methods can also specify reader properties through the readerProperties parameter. See examples in read. However, note that use of reader properties is more common in message sources, such as the HTTP listener, as shown in the CSV reader example.