csvstream
v1.1.0
Published
csv streaming parser
Downloads
152
Readme
csv stream
csv streaming parser to JSON.
Install
npm install csvstream
Sample
var csvstream = require('csvstream');
var fs = require('fs');
var rs = fs.createReadStream('./test/test1.csv');
var ws_head = fs.createWriteStream('./test/output2.json');
var csvs = new csvstream({header: true}, function(err, result) {
if(err)
console.error(err);
console.log(result);
// [
// { aaa: '1', bbb: '2', ccc: '3' },
// { aaa: '4', bbb: '5', ccc: '6' },
// { aaa: '7', bbb: '8', ccc: '9' }
// ])
});
rs.pipe(csvs).pipe(ws_head);
Options (csvstream(options))
Options are optional.
- encoding: default to
utf8
- delimiter: default to
,
- newlint: default to
\n
- quote: default to
\"
- header: default to
false
Example
Raw data
"aaa","bbb","ccc"
1, 2, 3
4, 5, 6
7, 8, 9
header
equals false you get:
[
["aaa","bb\"\"b","ccc"]
,["1","2","3"]
,["4","5","6"]
,["7","8","9"]
]
header
equals true you get:
[
{"aaa":"1","bbb":"2","ccc":"3"}
,{"aaa":"4","bbb":"5","ccc":"6"}
,{"aaa":"7","bbb":"8","ccc":"9"}
]
Best practice
Lint your csv files https://github.com/chilijung/csvlint.js before parsing :)
Example
var csvstream = require('../csvstream');
var csvlint = require('csvlint');
var fs = require('fs');
var rs = fs.createReadStream('./benchmark/100000.csv');
var ws = fs.createWriteStream('./benchmark/output2.json');
var csvlint = csvlint();
var csvs = csvstream({header: true});
csvlint.on('finish', function() {
console.log('csv lint is end');
})
csvs.on('finish', function() {
console.log('csv stream is end')
})
rs.pipe(csvlint).pipe(csvs).pipe(ws);
Tests
npm test
Inspired by
https://github.com/klaemo/csv-stream
License
MIT