test.csv file has:
"Id","UserName","Age"
"01","Sam Smith","33"
"02","Fred Frankly","44"
"03","Zachary Zupers","55"
acpected output: as Json File
[{"id":01,"User Name": " Sam Smith", "Age":"33"},
{"id":03,"User Name": " Fred Frankly", "Age":"44"}
{"id":03,"User Name": "Aachary Zupers", "Age":"55"}
]
I tried to solve like this using node.js
var fs = require("fs");
var data = fs.readFileSync('test.csv');
var stringData=data.toString();
console.log(stringData);
var arrayOne= stringData.split('\r\n');
var header=arrayOne[0].split(',');
var noOfRow=arrayOne.length;
var noOfCol=header.length;
var jArray=[];
var i=0,j=0;
for (i = 1; i < noOfRow-1; i++) {
for (j = 0; j< noOfCol; j++) {
var myNewLine=arrayOne[i].split(',');
jArray.push( '{'+header[j]+':'+myNewLine[j]+'}');
};
};
console.log( jArray);
this is the output I got when I run the above code: output Image In the above code I have just tried to show in json script. But If you can do that. Please provide the code to convert the displayed output into a .json file.
Please help me I shall be thankful to you.