CSV

◷ Reading Time: 4 minutes

toCsv

Creates a CSV document from a source input

source |toCsv (options)
  • source: null to create a new CSV document, otherwise a list of objects
  • options: The monad options is a set of key:value separated by ,
    • header: If header should be part of the content when serialized, then true, otherwise false. Default is true.
    • separator: Specifies the field value separator in each row of CSV. Default is ,
    • columns: List of columns in string or key/value pair of {name, property}
      • string: becomes name of the property
      • key/value pair: reads name of the header and creates property with provided value. Defines mapping between actual header name and resulted property name.
  • return: a CSV document that CSV monads can operate on
Example: null |toCsv (separator:'\t', header: false)
Result: Returns a new CSV document that field values are separated by Tab (\t) and header will not be written.
example: 'eg1.csv'|toCsv(separator:',', trim:true, columns:[{name:'St Day', property:'Date'}, 'Site_Name'])
Result: list of objects from file eg1.csv and only reads headers of St Date and Site_Name. In the result list all objects have properties of Date and Site_Name

csvWrite

Writes a collection of objects or a CSV document to a file.

source |csvWrite (path, options)
  • source: can be either a collection of objects or an existing CSV document
  • path: File path to write the CSV into
  • options: The monad options is a set of key:value separated by ,
    • header: If header should be part of the content when serialized, then true, otherwise false. Default is true.
    • separator: Specifies the field value separator in each row of CSV. Default is ,
    • columns: List of columns in string or key/value pair of {name, property}
      • string: becomes name of the property
      • key/value pair: reads name of the header and creates property with provided value. Defines mapping between actual header name and resulted property name.
Example: list |csvWrite ('test-file-csv', separator:'\t', columns: ['Age', 'Name'])

csvColumn

Creates or gets column on a CSV document

source |csvColumn (name, options)
  • source: can be an existing column or CSV document
  • name: Property
  • options: The monad options is a set of key:value separated by ,
    • formatter: Allows introducing a function that can format the values when Csv is serialized.
    • trim: True when values should be trimmed. Otherwise false.
    • headerName: name of the header, otherwise the property name is used
  • return: CSV document

Formatter function

The function should be in the following format:

FunctionName(ArgName)=Expression
  • FunctionName: A name for the formatter function
  • ArgName: The argument of the function which will be a place holder for the column value to be formatted. This function must have only one argument.
  • Expression: The behaviour using Expressions to define the formatting logic

Example

Define NameDate and Country columns on doc. Also set the formatter property for the Date column called fnDate which accepts the value of date as an argument named dt.

doc 
 |csvColumn('Name')
 |csvColumn('Date', 
    formatter: fnDate (dt)= 
            if dt!=null 
            then dt|formatDate(dd/MM/yyyy) 
            else null
    )
 |csvColumn('Country')

csvAdd

Adds a record or object to a CSV document

doc |csvAdd (record)
  • doc: is a CSV document
  • record: is a single or a collection of objects to be added to the CSV document
  • return: CSV document
Updated on February 11, 2020

Was this article helpful?

Related Articles