csv-file-to-json
v4.0.5
Published
Converts a csv file to a json.
Downloads
6,352
Maintainers
Readme
Reads a csv file or data variable having a table and returns an array of obects. In which each object consists of all headers as keys and there data as values.
Installation
npm i --save csv-file-to-json
Usage
csv-file-to-json
support CommonJS.
In module system
Use filePath
key to provide CSV data from file
.
const csvToJson = require("csv-file-to-json");
const dataInJSON = csvToJSON({ filePath: "./filePath.csv" });
Use data
key to provide CSV data from variable or directly
.
const csvToJSON = require("csv-file-to-json");
const dataInJSON = csvToJSON({ data: someCSVData });
Sample input (csv data) :
FIRST_NAME,LAST_NAME,NUMBER,EMAIL,ADDRESS
Debra,Burks,880012XXXX,[email protected],"9273 Thome Ave., `Orchard Park`, NY - 14127"
Kasha,Todd,null,[email protected],"910, Vine Street!!!, (Campbell), CA - 95008"
Tameka,Fisher,8800111XXX,null,"7693 ~ Honey Creek St., Redondo Beach, "CA" 90278"
Sample output (json data) :
[
{
FIRST_NAME: 'Debra',
LAST_NAME: 'Burks',
NUMBER: '880012XXXX',
EMAIL: '[email protected]',
ADDRESS: '9273 Thome Ave., `Orchard Park`, NY - 14127'
},
{
FIRST_NAME: 'Kasha',
LAST_NAME: 'Todd',
NUMBER: null,
EMAIL: '[email protected]',
ADDRESS: '910, Vine Street!!!, (Campbell), CA - 95008'
},
{
FIRST_NAME: 'Tameka',
LAST_NAME: 'Fisher',
NUMBER: '880111XXXX',
EMAIL: null,
ADDRESS: '7693 ~ Honey Creek St., Redondo Beach, "CA" 90278'
}
]
Use separator
key to specify your separator. Default separator is ","
.
const dataInJSON = csvToJSON({ filePath: "./filePath.txt", separator: "," });
Use hasHeader
key to specify if your file or data contains a header line or not.
Default is true and first line of csv will be considered as header
const dataInJSON = csvToJSON({ filePath: "./filePath.txt", hasHeader: true });
Use headers
key to specify your own headers.
const dataInJSON = csvToJSON({ filePath: "./filePath.txt", hasHeader: false, headers: ["FIRST_NAME", "LAST_NAME", "NUMBER", "EMAIL", "ADDRESS"] });
References
- To convert text and csv both to json. Try - https://www.npmjs.com/package/data-to-json
- To convert only txt data to json. Try - https://www.npmjs.com/package/txt-file-to-json