@omarelebiary/simple-csv-parser
v1.0.0
Published
A Javascript csv parser
Downloads
1
Readme
csv-parser
A Simple CSV file parser.
csv-parser
can convert CSV into JSON or array.
Usage
Simply instantiate CSVParser
and put a csv file to it and get the rows out
Example
Let's say that we have a CSV file data1.csv
:
full name, AGE, STATUS
John Doe 1, 24, ACTIVE
John Doe 2, 22, BLOCKED
John Doe 3, 25, ACTIVE
You can parse it like this:
const Parser = require('./CSVParser');
let parser = new Parser('test/dataset/data1.csv', {delimiter: ',', headers: true});
parser.parseCSV().then((data) => {
console.log(data);
});
Output:
[ { 'full name': 'John Doe 1',
' AGE': ' 24',
' STATUS': ' ACTIVE' },
{ 'full name': 'John Doe 2',
' AGE': ' 22',
' STATUS': ' BLOCKED' },
{ 'full name': 'John Doe 3',
' AGE': ' 25',
' STATUS': ' ACTIVE' } ]
The returned data is a JSON object. Header is used as the property name of the object.
The csv constructor accepts the following options
let parser = new Parser('path to file', {options});
options {
delimiter: ',', // Specify the delimiter to be used. Default: ','
headers: true // Use first row as keys in JSON. Default: false
}