Skip to main content

CSV

Functions for reading, parsing, and producing CSV data.

For importing CSV data into Xurrent, see the import_csv function.

arrayToCsvString

Convert an array of objects to a CSV string with the properties of the first object as header.

let csvString = arrayToCsvString(listOfObjects);

csvToObjectList

Parses a string containing CSV data and converts it into an array of objects. The first row of the CSV is used as the header row, with each column name becoming a property key in the resulting objects.

csvToObjectList(csvString);
csvToObjectList(csvString, options);

For large CSV files, consider using the file_read_csv function instead.

Parameters:

  • csvString - The CSV string to parse
  • options - Optional configuration object with the following properties:
    • delimiter - Field delimiter character(s). Can be one or several characters. Defaults to , (comma).
    • rowDelimiter - Character(s) used to delimit records. Defaults to auto discovery if not provided. Supported auto discovery methods are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.
    • max_limit_on_data_read - Maximum number of characters allowed in field and line buffers before an exception is raised.
    • quote - Character used to surround fields (single character only). Set to null, false or empty to disable. Defaults to double quote (").
    • trim - If true, ignore whitespace immediately around the delimiter. Defaults to false. Does not remove whitespace in quoted fields.

Example 1: Parse CSV

let csv = `name,age,city
Franz,30,Vienna
Maria,25,Graz`;
let objects = csvToObjectList(csv);
// Result: [
// { name: 'Franz', age: '30', city: 'Vienna' },
// { name: 'Maria', age: '25', city: 'Graz' }
// ]

Example 2: Parse CSV with custom options

let csv = `name; age; city
Franz; 30 ; Vienna
Maria ; 25; Graz`;
let options = {
delimiter: ";",
trim: true,
};
let objects = csvToObjectList(csv, options);
// Result: [
// { name: 'Franz', age: '30', city: 'Vienna' },
// { name: 'Maria', age: '25', city: 'Graz' }
// ]

Example 3: Parsing standard CSV export

let account = "wdc";
let result = initExport("teams", account);
let exportJob = null;

for (let i = 0; i < 50; i++) {
sleep(2000);

exportJob = checkExportState(result.token, account);
if (exportJob.state == "done" || exportJob.state == "error") {
break;
}
}

if (exportJob.state == "error") {
log("Export failed", exportJob);
return;
}

let csv = loadFileToString(exportJob.url);
let objects = csvToObjectList(csv);

file_read_csv

Reads a CSV file from a file repository line by line, parsing each line into an object and passing it to a callback.

file_read_csv(file_account, file_name, options, (obj) => {
// process line object...
return true;
});

The line callback function receives a JSON object which keys correspond to the column names of the CSV file.

Return false from the callback function to stop file reading.

Possible options

The options parameter is optional. When it is present, it should have all three of the following properties:

  • col_sep: column separator - default : ,
  • line_sep: line separator - default : \n
  • string_sep: string separator - default : "

Example 1:

let fileAccount = "file-rep";
let fileName = "csv_test_file.txt";
let count = 0;

file_read_csv(fileAccount, fileName, (obj) => {
count++;
if (count % 100 == 0) {
log(`Processed ${count} lines`);
}

// Do something with the line object...

return true;
});

Example 2: Call with options

let fileAccount = "file-rep";
let fileName = "csv_test_file.txt";
let options = {
col_sep: ",",
line_sep: "\n",
string_sep: '"',
};
file_read_csv(fileAccount, fileName, options, (obj) => {
// Do something with the line object...
});