What is csv-parse?
The csv-parse package is a flexible Node.js library that provides a parser converting CSV text input into arrays or objects. It implements the Node.js stream.Transform API. It is also capable of converting large datasets and supports many advanced features such as streaming and asynchronous processing.
What are csv-parse's main functionalities?
Parsing CSV to Arrays
This feature allows you to parse CSV data into arrays. Each row in the CSV data becomes an array.
const parse = require('csv-parse');
const assert = require('assert');
const input = 'a,b,c\nd,e,f';
parse(input, function(err, output){
assert.deepEqual(output, [['a', 'b', 'c'], ['d', 'e', 'f']]);
});
Parsing CSV with Column Mapping
This feature allows you to map CSV columns to object properties, so each row in the CSV data becomes an object with named properties.
const parse = require('csv-parse');
const assert = require('assert');
const input = 'a,b,c\nd,e,f';
const parser = parse({columns: true}, function(err, records){
assert.deepEqual(records, [{a: 'd', b: 'e', c: 'f'}]);
});
parser.write(input);
parser.end();
Asynchronous Iteration
This feature allows for asynchronous iteration over the parsed records, which is useful for handling large CSV files or streams.
const parse = require('csv-parse');
const fs = require('fs');
const parser = fs.createReadStream('/path/to/csv-file.csv').pipe(parse({columns: true}));
(async () => {
for await (const record of parser) {
// Work with each record
}
})();
Other packages similar to csv-parse
papaparse
Papa Parse is a powerful CSV parser that can handle large files and malformed input gracefully. It works in both browser and server environments. Compared to csv-parse, Papa Parse has a more user-friendly API and can automatically detect delimiters.
fast-csv
Fast-csv is another CSV parsing and formatting library for Node.js. It provides a simple API and supports both stream and callback-based processing. It is known for its speed and efficiency, but csv-parse offers more advanced features and customization options.
csvtojson
csvtojson is a full-featured CSV parser library that converts CSV to JSON. One of its main differences from csv-parse is that it focuses on the JSON output format, whereas csv-parse provides more flexibility in handling the parsed data.
CSV parser for Node.js and the web
The csv-parse
package is a parser converting CSV text input into arrays or objects. It is part of the CSV project.
It implements the Node.js stream.Transform
API. It also provides a simple callback-based API for convenience. It is both extremely easy to use and powerful. It was first released in 2010 and is used against big data sets by a large community.
Documentation
Main features
- Flexible with lot of options
- Multiple distributions: Node.js, Web, ECMAScript modules and CommonJS
- Follow the Node.js streaming API
- Simplicity with the optional callback API
- Support delimiters, quotes, escape characters and comments
- Line breaks discovery
- Support big datasets
- Complete test coverage and lot of samples for inspiration
- No external dependencies
- Work nicely with the csv-generate, stream-transform and csv-stringify packages
- MIT License
Usage
Run npm install csv
to install the full CSV module or run npm install csv-parse
if you are only interested by the CSV parser.
Use the callback and sync APIs for simplicity or the stream based API for scalability.
Example
The API is available in multiple flavors. This example illustrates the stream API.
import assert from 'assert';
import { parse } from 'csv-parse';
const records = [];
const parser = parse({
delimiter: ':'
});
parser.on('readable', function(){
let record;
while ((record = parser.read()) !== null) {
records.push(record);
}
});
parser.on('error', function(err){
console.error(err.message);
});
parser.on('end', function(){
assert.deepStrictEqual(
records,
[
[ 'root','x','0','0','root','/root','/bin/bash' ],
[ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]
]
);
});
parser.write("root:x:0:0:root:/root:/bin/bash\n");
parser.write("someone:x:1022:1022::/home/someone:/bin/bash\n");
parser.end();
Contributors
The project is sponsored by Adaltas, an Big Data consulting firm based in Paris, France.