CSVParse for Node.js

IssuesGitHub

Option skip_records_with_error

The skip_records_with_error option tolerates parsing errors. It skips the records containing an error inside and directly go process the next record.

Be careful, this functionality can not suit every data set. It implies a good knowledge in your data in the sense that you must be confident that no field contains any record delimiters. By nature, CSV fields can contains records delimiters if quoted. On error, the parser has no indication to know if a record delimiter is one or if it is inside a quoted field or not. Thus, using this option confidently implies that your fields do not contain any records delimiter inside.

A skip event is emitted when an error is found and when the record is skipped. The error object is passed as the first argument of the event callback and it can expose additional information depending on the type of errors. The error documentation list of the error types as well as the contextual properties they expose.

Listening to the skip event

The option.skip_records_with_error.js example catch an invalid closing quote error and continue parsing the next records.

import assert from 'node:assert';
import { parse } from 'csv-parse';

const parser = parse({
  skip_records_with_error: true
}, function(err, records){
  assert.deepStrictEqual(
    records, [
      ['a', 'b', 'c'],
      ['d', 'e', 'f'],
      ['h', 'i', 'j']
    ]
  );
});
parser.on('skip', function(err){
  assert(/^Invalid Closing Quote/.test(err.message));
});
parser.write(`
"a","b","c"
"d","e","f"
"invalid"," " ","record"
"h","i","j"
`.trim());
parser.end();

About

The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.