Skip to content

$fn.csvToObject(buf, options)

Converts specified CSV file data into object format.

Arguments

buf

NameTypeRequiredDescription
bufBufferYesBuffer data of the CSV file

options

Added in v4.2.0.

PropertyTypeRequiredDefaultDescription
columnsstring[]NoSpecifies 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.
fromLinenumberNo1Specifies which line of the CSV file to start treating as records. The first line is treated as 1.
relaxColumnCountbooleanNofalseWhen 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.
relaxColumnCountLessbooleanNofalseWhen 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.
relaxColumnCountMorebooleanNofalseWhen 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.
skipEmptyLinesbooleanNotrueWhen true, empty lines are skipped. When false, empty lines are processed as objects with the first property as an empty string.
encodingstringNoutf-8Specifies the character encoding of the CSV file. Supports utf-8 and shift_jis.

CSV File Specifications

Header rowRequired
Becomes the property names of the object. (When columns is specified, the columns content becomes the property names)
Example: “email”, “name”
Header name constraintsWhen using header names as property names, header names starting with a number are not allowed
Delimiter, (comma)
FieldIf a value contains double quotes, it must be escaped with another double quote. (RFC 4180 compliant)
Example: "aaa","b""bb","ccc"
Line breakCRLF or LF
Character encodingUTF-8, Shift_JIS
BOMWith or without BOM

Return value

TypeDescription
array of objectReturns 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
});