Lightweight CSV to JSON for Node.JS
Usage
var csv = ; var filename = './path/to/your.csv'; // OR this can be a URL like // filename = 'http://sample.com/data.csv'; // OR filename can be a string of CSV data // // Suppose your.csv is following: // // Date,Open,High,Low // 2015-03-27,2.58,2.65,2.53 // 2015-03-26,2.73,2.74,2.57 // 2015-03-25,2.64,2.73,2.61 csv // Detects whether filname is // a local file, URL, or string of data ;
Or you can be specific
var promiseFile = csv; var promiseURL = csv; var promiseStr = csv;
Get Started
npm install node-csvjsonlite
var csv = ;
Features
Bluebird as promise
Bluebird is used as promise to keep it lightweight.
Commas within double quotes
var csv = ; var filename = './path/to/your.csv'; // where your.csv has the following data: // Date, Value // 2015-02-14,"2,2" // 2015-02-15,"4,3" // 2015-02-16,"1,2" // resolves to // [{ Date: 2015-02-14, Value: 2,2 }, // { Date: 2015-02-15, Value: 4,3 }, // { Date: 2015-02-16, Value: 1,2 }]
CSV from online source
var csv = ; var filename = 'http://something.com';
where filename can be HTTP or HTTPS.
Note: If file does not exist, then it will reject the promise.
Error Handling
Error will be resolved in the Promise rejection.
var csv = ; var badFilename = './path/to/invalid.csv'; csv ;
Note that since it uses bluebird as promise, if the promise is rejected and there is no rejection handler, it will throw an error.