pfx-csv:marshal

Overview

Converts a List<Map<String, String>> (from the exchange body) into a CSV string.

Input: The exchange body must be a List<Map<String, String>>.

Output: The exchange body is replaced with the CSV string.

Header resolution:

  • If header is set, those column names are used (and values are extracted from maps in that order).

  • If header is not set, the keys of the first map are used.

Split awareness: When forceSkipHeaderWhenPartOfSplit=true (the default) and the exchange has SPLIT_INDEX > 0, the header line is suppressed. This prevents duplicate header lines when marshalling batches that are later appended to a single file.

Properties

Parameter

Type

Default

Description

format

String

DEFAULT

Base CSVFormat preset name (e.g. DEFAULT, EXCEL, TDF, RFC4180).

delimiter

String

,

Field delimiter character. Supports Java escape sequences (e.g. \\t for tab).

header

String

(auto-detect)

Comma-separated list of column names. When omitted, keys from the first map in the body are used.

skipHeaderRecord

Boolean

false

Whether to omit the header line from output.

quoteCharacter

Character

"

Character used to quote field values.

quoteDisabled

Boolean

false

Set to true to disable quoting entirely.

quoteMode

String

(none)

Apache Commons CSV QuoteMode: ALL, ALL_NON_NULL, MINIMAL, NON_NUMERIC, NONE.

escapeCharacter

Character

(none)

Escape character for special characters inside fields.

commentMarker

Character

(none)

Character that marks comment lines.

headerComments

String

(none)

Comma-separated list of comment lines to write before the header.

recordSeparator

String

Platform default

Record (line) separator. Accepts tokens CR, LF, CRLF (e.g. CRLF for Windows line endings).

nullString

String

(none)

String to write for null map values. Without this, null values are written as empty fields.

trim

Boolean

(none)

Whether to trim leading/trailing whitespace from field values.

ignoreSurroundingSpaces

Boolean

(none)

Whether to ignore spaces surrounding field values.

trailingDelimiter

Boolean

(none)

Whether to add a trailing delimiter at the end of each record.

camelSplitIndexAware

Boolean

true

Uses the Camel SPLIT_INDEX exchange property for split-batch behavior.

forceSkipHeaderWhenPartOfSplit

Boolean

true

When true and the exchange is part of a split (batch index > 0), the header line is automatically skipped to avoid duplicating it in every batch.

lazyStartProducer

Boolean

false

(Advanced) Whether to defer producer creation until the first message is processed.

Examples

Basic CSV Export

Fetch data from Pricefx and write it as CSV:

XML
<route id="exportToCsv">
    <from uri="timer://fetchData?repeatCount=1"/>
    <to uri="pfx-api:fetch?filter=myFilter&amp;objectType=PX"/>
    <to uri="pfx-csv:marshal"/>
    <to uri="file:export?fileName=data.csv"/>
</route>

Semicolon-Delimited with Explicit Quote Mode

Common for European locale CSV files:

XML
<to uri="pfx-csv:marshal?delimiter=;&amp;quoteMode=ALL"/>

Batched Export with Header Only on First Batch

When splitting fetched data into batches and appending to a file, forceSkipHeaderWhenPartOfSplit (default true) ensures the header appears only once:

XML
<route id="batchedExport">
    <from uri="timer://fetchData?repeatCount=1"/>
    <to uri="pfx-api:fetch?filter=myFilter&amp;objectType=PX&amp;batchedMode=true&amp;batchSize=5000"/>
    <split>
        <simple>${body}</simple>
        <to uri="pfx-api:fetchIterator"/>
        <to uri="pfx-csv:marshal"/>
        <to uri="file:export?fileName=data.csv&amp;fileExist=Append"/>
    </split>
</route>

Custom Record Separator (Windows CRLF)

XML
<to uri="pfx-csv:marshal?recordSeparator=CRLF"/>

Common Pitfalls

  1. Delimiter in XML -- Remember to XML-escape the ampersand in URI query strings: use &amp; not &. For the tab character, use \\t (double-escaped in XML).

  2. Quoting disabled with embedded delimiters -- Set quoteDisabled=true only if you are certain that no field values contain the delimiter character. When quoting is disabled, fields with embedded delimiters will corrupt the output.

  3. Null handling -- Without nullString, null values are written as empty fields. Use nullString=NULL to write the literal string NULL for null map values.

  4. Duplicate headers in batched exports -- If you set forceSkipHeaderWhenPartOfSplit=false and marshal inside a split, every batch will include the header line. This produces an invalid CSV when batches are appended to one file.