@novigi/csv-to-json
Simple library for csv parse to Javascript object from any CSV string or csv file
πΏ Features
- Chainable and immutable API
- Parse CSV content into JSON object
- Parse CSV files into JSON objects
- Supports custom headers
π¦ Getting Started
- Install the dependency
npm install @novigi/csv-to-json
- Import the library
const lib = require('@novigi/csv-to-json');
π Documentation
csv-to-json
This library contains methods that allow user to parse the csv, either it is a csv string or file convert into the Javscript object.
This guideline is about the formats and examples that can follow for csv string parsing to Javascript object conversion
const csv = require('@novigi/csv-to-json')
const input = 'name,age,sex\nkasun,33,male\nsteve,35,male'
let json = csv().parseData(input)
// or
let json = csv().parseFile(path/to/file)
Chainable + immutable methods!
-
csv-to-json
-
~Csv
-
.delimiter β
Csv
-
.lineBreak β
Csv
-
.noHeaders β
Csv
-
.headers β
Csv
-
.parseFile β
Csv
-
.parseData β
Csv
-
.delimiter β
-
~csv([options]) β
Csv
-
~Csv
csv-to-json~Csv
Kind: inner class of csv-to-json
-
~Csv
-
.delimiter β
Csv
-
.lineBreak β
Csv
-
.noHeaders β
Csv
-
.headers β
Csv
-
.parseFile β
Csv
-
.parseData β
Csv
-
.delimiter β
Csv
csv.delimiter β Delimiter is for different separators that can manually pass with csv file or string, the separator separates the CSV string in the context.
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
delimiter | string |
The delimiter as a parameter to separate the values in the csv |
Example
const csvString = "name;age;sex\nkasun;33;male\nsteve;35;male"
csv().fieldDelimiter(';').parseData(csvString) // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]
Csv
csv.lineBreak β Line-break is for for indicating the ending of a row in the csv content.
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
lineBreak | string |
The lineBreak as a parameter to separate the lines in the csv |
Example
const csvString = "name,age,sex\r\nkasun,33,male\r\nsteve,35,male"
csv().lineBreak('\r\n').parseData(csvString) // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]
Csv
csv.noHeaders β This method is to indicate whether csv content has the header data or not.
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
noHeaders | * |
Indicate whether csv content has headers. Undefined parameter is considered as true
|
Example
const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().noHeaders().headers(customHeader).parseData(csvString) // custom headers wil be used as the headers of the csv content
Csv
csv.headers β Adds headers to use as the headers of the csv content. Specifying headers will override the headers in the csv content. When provided header list is insufficient, headers in the csv content are considered
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
headers | array |
Custom header array to be used as headers of the csv content |
Example
const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().headers(customHeader).parseData(csvString)
Csv
csv.parseFile β CSV file read and parse to javascript data structure , 'parseFile' method read and convert the csv file into the javascript array object.
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
csvFile | string |
valid csv file path , the path passes as a parameter and read the file in that file is exist |
Example
const path = './resources/csvFile.csv' // csv file directory path
csv().parseFile(csvFile) // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]
Csv
csv.parseData β CSV parse to javascript data structure,'parse' method convert the csv string to the javascript array object.
Kind: instance property of Csv
Returns: Csv
- new instance of Csv
object with the same options
Param | Type | Description |
---|---|---|
csvString | string |
valid string to passes as a parameter |
Example
const csvString = "name,age,sex\nkasun,33,male\nsteve,35,male"
csv().parseData(csvString) // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]
Csv
csv-to-json~csv([options]) β Create an Csv chain builder instance
Kind: inner method of csv-to-json
Returns: Csv
- new instance of 'Csv' object
Param | Type | Description |
---|---|---|
[options] | object |
optional configurations for the chain |
[options.delimiter] | string |
delimiter which csv fields are separated |
[options.lineBreak] | string |
line break character of csv rows |
[options.noHeaders] | boolean |
falg to indicate csv content has a header row or not |
[options.headers] | object |
custom headers for parsing csv content |
Example
csv()
csv(options)
This is an auto generated file. Please don't make changes manually