CSV controller

Availability table

Controller availability

Module

Available

module.scripts

Supported feature

module.keycloak

Supported feature

csv controller

This controller provides utility functions to parse CSV content into JSON.

It supports configurable delimiters, optional header mapping, line trimming, comment markers, escaping/quoting, and row filtering such as skipping leading noise lines or trailing footer records.

The controller allows obtaining a new CsvParserBuilder. This builder provides functions to configure how the CSV will be parsed. Once the configuration is complete, calling csv.CsvParserBuilder.build() returns a CsvParser, which can parse CSV data from a string.

csv.newCsvParserBuilder()

Create a new CsvParserBuilder() with default options

Returns:

A CsvParserBuilder() instance used to configure and build a parser

CsvParser

class csv.CsvParser()

Parser created from a CsvParserBuilder() to parse CSV data into JSON

CsvParser.parse(csv: string)

Parse the entire CSV string and return all records as a JSON array

Arguments:
  • csv – CSV content to parse

Returns:

executionResult where message contains the JSON array when successful

CsvParser.parseIterator(csv: string)

Create an iterator that yields CSV records one by one as JSON

Arguments:
  • csv – CSV content to parse

Returns:

A CsvParserIterator() over JSON records. Call csv.CsvParserIterator.close() when done

CsvParser.parseStream(csv: string, batchSize: number, consumer: any)

Parse CSV data and stream results in batches to the provided consumer

Arguments:
  • csv – CSV content to parse

  • batchSize – Maximum number of records per batch. Values <= 0 default to 1

  • consumer – Callback invoked with a JSON array string for each batch

Returns:

executionResult indicating whether parsing completed successfully

CsvParserBuilder

class csv.CsvParserBuilder()

Builder used to configure CSV parsing options and create a CsvParser()

CsvParserBuilder.build()

Build a CsvParser() using the current builder configuration

Returns:

A CsvParser() configured with the current options

CsvParserBuilder.setCommentMarker(commentMarker: string)

Set a comment marker character used to ignore commented lines

Arguments:
  • commentMarker –

    Character that marks the start of a comment line

    Default: null

Returns:

This builder instance for chaining

CsvParserBuilder.setDelimiter(delimiter: string)

Set the field delimiter character used to split columns

Arguments:
  • delimiter –

    Delimiter character to use between fields (for example , or ;)

    Default: ;

Returns:

This builder instance for chaining

CsvParserBuilder.setEscapeChar(escapeChar: string)

Set the escape character used to escape special characters in values

Arguments:
  • escapeChar –

    Character used to escape delimiters, quotes, or line terminators

    Default: null

Returns:

This builder instance for chaining

CsvParserBuilder.setFieldNames(fieldNames: string[])

Defines explicit column header names.

Header resolution is applied after skipHeaderLines.

When not set:

  • The first non-skipped line (after skipHeaderLines) is used as header names

When set:

  • The provided names are used as keys for mapping each record

  • No line from the CSV is used as headers

  • All lines, including the first non-skipped line, are treated as data

The number of provided header names should match the number of columns in the CSV.

Arguments:
  • fieldNames –

    Ordered list of column names used to map each record

    Default: null (use first CSV line as headers)

Returns:

This builder instance for chaining

CsvParserBuilder.setLineTerminator(lineTerminator: string)

Set a custom line terminator for CSV records

Arguments:
  • lineTerminator –

    Line separator string used to split records (for example \n or \r\n)

    Default: \n

Returns:

This builder instance for chaining

CsvParserBuilder.setQuoteChar(quoteChar: string)

Set the quote character used to wrap values

Arguments:
  • quoteChar –

    Character used to quote values that contain delimiters or newlines

    Default: null

Returns:

This builder instance for chaining

CsvParserBuilder.setSkipEmptyLines(skipEmptyLines: boolean)

Enable or disable skipping of empty lines

Arguments:
  • skipEmptyLines –

    true to skip lines where all fields are blank

    Default: true

Returns:

This builder instance for chaining

CsvParserBuilder.setSkipFooterLines(skipFooterLines: number)

Skip a number of trailing data rows at the end of parsing

Arguments:
  • skipFooterLines –

    Number of rows to drop from the end of the CSV

    Default: 0

Returns:

This builder instance for chaining

CsvParserBuilder.setSkipHeaderLines(skipHeaderLines: number)

Skip a number of lines before any CSV parsing or header handling

Arguments:
  • skipHeaderLines –

    Number of lines to drop from the start of the input before parsing

    Default: 0

Returns:

This builder instance for chaining

CsvParserBuilder.setSkipIncompleteLines(skipIncompleteLines: boolean)

Enable or disable skipping of records with fewer fields than expected

Arguments:
  • skipIncompleteLines –

    True to drop records that have fewer fields than the header or first record

    Default: true

Returns:

This builder instance for chaining

CsvParserBuilder.setThrowOnIncompleteLines(throwOnIncompleteLines: boolean)

Control whether incomplete records should raise an error when they are not skipped

Arguments:
  • throwOnIncompleteLines –

    Controls how incomplete records are handled.

    • True: throw an error when a record has missing fields

    • False: keep the record and set missing fields to null

    This parameter has no effect if csv.CsvParserBuilder.setSkipIncompleteLines() is enabled, as incomplete records are skipped entirely.

    Default: true

Returns:

This builder instance for chaining

CsvParserBuilder.setTrim(trim: boolean)

Enable or disable trimming of leading and trailing whitespace in values

Arguments:
  • trim –

    true to trim values, false to keep whitespace

    Default: true

Returns:

This builder instance for chaining

CsvParserIterator

class csv.CsvParserIterator()

Iterator that streams CSV records as JSON strings and manages parser resources

CsvParserIterator.close()

Close the iterator and release underlying parser resources

Returns:

No return value

CsvParserIterator.hasNext()

Check if another record is available. When exhausted, the iterator closes itself

Warning: this method may throw an exception when setThrowOnIncompleteLines is enabled, as it validates the next record before confirming availability

Returns:

true if there is another record, false otherwise

CsvParserIterator.next()

Return the next record as a JSON string

Returns:

JSON representation of the next record, or null if none is available