$fn.csvToObject(buf, options)
Converts specified CSV file data into object format.
Arguments
buf
| Name | Type | Required | Description |
|---|---|---|---|
| buf | Buffer | Yes | Buffer data of the CSV file |
options
Added in v4.2.0.
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
| columns | string[] | No | Specifies the property names of the output object in column order. Columns with false / null / undefined specified in the array elements will not be included in the output object. | |
| fromLine | number | No | 1 | Specifies which line of the CSV file to start treating as records. The first line is treated as 1. |
| relaxColumnCount | boolean | No | false | When true, no error occurs even if the number of columns in a CSV record does not match the expected number of columns (number of elements in columns). If there are fewer columns, the corresponding properties are not output; if there are more columns, the extra values are discarded. |
| relaxColumnCountLess | boolean | No | false | When true, no error occurs even if the number of columns in a CSV record is less than the expected number. If there are more columns, an exception is thrown. |
| relaxColumnCountMore | boolean | No | false | When true, no error occurs even if the number of columns in a CSV record is more than the expected number. If there are fewer columns, an exception is thrown. |
| skipEmptyLines | boolean | No | true | When true, empty lines are skipped. When false, empty lines are processed as objects with the first property as an empty string. |
| encoding | string | No | utf-8 | Specifies the character encoding of the CSV file. Supports utf-8 and shift_jis. |
CSV File Specifications
| Header row | Required Becomes the property names of the object. (When columns is specified, the columns content becomes the property names)Example: “email”, “name” |
| Header name constraints | When using header names as property names, header names starting with a number are not allowed |
| Delimiter | , (comma) |
| Field | If a value contains double quotes, it must be escaped with another double quote. (RFC 4180 compliant) Example: "aaa","b""bb","ccc" |
| Line break | CRLF or LF |
| Character encoding | UTF-8, Shift_JIS |
| BOM | With or without BOM |
Return value
| Type | Description |
|---|---|
| array of object | Returns CSV file data converted to object format |
Sample
Example using header strings directly as property names
const files = await $fn.getUploadFiles();const csv_buf = files[0].data;const obj = $fn.csvToObject(csv_buf);Example specifying separate property names using columns and fromLine
const obj = $fn.csvToObject(csv_buf, { columns: ["id", "name", "amount"], fromLine: 2});