🆕parseCSV

To return a structured list or array of rows and data from specified CSV text

The parseCSV function parses CSV-formatted text into a structured list or array. You can customize parsing behavior by setting headers, separators, and text quote characters.

Syntax

parseCSV(string, json)

Parameters

  • data (string): The CSV text input. Example: "Name,Age\nKiran,42\nLisa,27"

  • options (JSON object): Optional configuration, with the following properties:

    • firstLineIsHeader: boolean, default is false. If true, the first line is treated as column headers.

    • separator: string. Sets the delimiter between fields, such as "," or ";". Auto-detects if not specified.

    • textQuote: string. Specifies the character used to quote text fields. Default is " (double quotes).

Return

[JSON]: A list or an array of JSON objects, with keys from the header if firstLineIsHeader is true.

[[text]]: A list or an array of text arrays (rows), if firstLineIsHeader is false.

Examples

1. Default firstLineIsHeader=false

Example:

parseCSV("Name,Age
Kiran,42
Lisa,27", {})

Or explicitly set firstLineIsHeader to false:

parseCSV("Name,Age
Kiran,42
Lisa,27", {firstLineIsHeader: false})

Result:

[
  ["Name", "Age"],
  ["Kiran", "42"],
  ["Lisa", "27"]
]

2. Using firstLineIsHeader=true

Example:

parseCSV("Name,Age
Kiran,42
Lisa,27", {firstLineIsHeader: true})

Result:

[
  {
    Name: "Kiran",
    Age: "42"
  },
  {
    Name: "Lisa",
    Age: "27"
  },
]

3. Custom separator

Note: If no separator is specified, parseCSV will auto-detect a common delimiter like commas or tabs based on the input structure.

Example:

parseCSV("Name;Age
Kiran;42
Lisa;27", {separator: ";", firstLineIsHeader: true})

Result:

[
  {
    Name: "Kiran",
    Age: "42"
  },
  {
    Name: "Lisa",
    Age: "27"
  },
]

4. Specifying textQuote

Note: Use textQuote to specify a character for quoting fields. It's optional to quote every field; parseCSV will still correctly read unquoted fields.

In this example, text fields are quoted with single quotes.

Example:

parseCSV("Name,Age
Kiran,42
'Lisa',27", {firstLineIsHeader: true})

Result:

[
  {
    Name: "Kiran",
    Age: "42"
  },
  {
    Name: "'Lisa'",
    Age: "27"
  },
]

5. Using custom textQuote character

You can set a textQuote character, such as a single quote ('), to handle fields with quoted text specifically.

Example:

parseCSV("Name,Age
Kiran,42
'Lisa',27", {textQuote: "'", firstLineIsHeader: true})

Result:

[
  {
    Name: "Kiran",
    Age: "42"
  },
  {
    Name: "Lisa",
    Age: "27"
  },
]

Last updated