Options
The Options
object describes the configuration available for the operation of parsing CSV files using the csv.parse
function and the csv.Parser
class.
Properties
asObjects
When asObjects
is set to true
, parsing operations over the CSV file will return an array of objects. The object keys are the column names from the CSV file, as inferred from the first line of the file, and the values are the field values from the CSV record. Note that the first line of the CSV file is skipped, as it is assumed to contain the column names (header row).
As a result, the following CSV file:
name,age
John,30
Jane,25
Will be parsed into the following array of objects:
[
{ "name": "John", "age": "30" },
{ "name": "Jane", "age": "25" }
]
Example
import { open } from 'k6/experimental/fs';
import csv from 'k6/experimental/csv';
const file = await open('data.csv');
const parser = new csv.Parser(file, {
delimiter: ',',
skipFirstLine: true,
fromLine: 2,
toLine: 8,
});
export default async function () {
// The `next` method attempts to read the next row from the CSV file.
//
// It returns an iterator-like object with a `done` property that indicates whether
// there are more rows to read, and a `value` property that contains the row fields
// as an array.
const { done, value } = await parser.next();
if (done) {
throw new Error('No more rows to read');
}
// We expect the `value` property to be an array of strings, where each string is a field
// from the CSV record.
console.log(done, value);
}