data-to-json
v1.0.5
Published
Reads a text file, 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.
Downloads
216
Maintainers
Readme
Reads a text file, 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. Can also filter data on given number of records and returns only required data in array.
Installation
npm i --save data-to-json
Usage
data-to-json
support CommonJS.
In module system
Use filePath
key to provide TEXT or CSV data from a file
.
const dataToJson = require("data-to-json");
Use .txt
to use TXT data.
const dataInJSON = dataToJson.txt({ filePath: "./filePath.txt" }).toJson();
OR
Use .csv
to use CSV data.
const dataInJSON = dataToJson.csv({ filePath: "./filePath.csv" }).toJson();
Use data
key to provide TEXT data from variable or directly
.
const dataInJSON = dataToJson.txt({ data: someTEXTData }).toJson();
OR
const dataInJSON = dataToJson.csv({ data: someCSVData }).toJson();
Sample input (txt 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 880111XXXX null 7693 ~ Honey Creek St., Redondo Beach, "CA" 90278
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 .records
to get required number of rows.
const dataInJSON = dataToJson.txt({ filePath: "./filePath.txt" }).records(1).toJson();
OR
const dataInJSON = dataToJson.csv({ filePath: "./filePath.csv" }).records(1).toJson();
Sample output when .records
is 1
(json data) :
[
{
FIRST_NAME: 'Debra',
LAST_NAME: 'Burks',
NUMBER: '880012XXXX',
EMAIL: '[email protected]',
ADDRESS: '9273 Thome Ave., `Orchard Park`, NY - 14127'
}
]
References
- To convert only text to json. Try - https://www.npmjs.com/package/txt-file-to-json
- To convert only csv to json. Try - https://www.npmjs.com/package/csv-file-to-json