Abhinav Abhinav - 3 months ago 18
Node.js Question

nodejs - csvtojson not giving proper json keys in output

I am using

csvtojson
package in
nodejs
. I have written below code to convert my
csv
file but it is not converting into
json
properly. The keys in
json
do not have double brackets hence I am not able to get value using key.

var Converter = require("csvtojson").Converter;
var converter = new Converter({});

converter.fromFile("./file.csv", function(err,result){
console.log(result);
});


Output shown is as below:

[ { a: 1, b: 2, c: 3 }, { a: 3, b: 4, c: 5 } ]


Hence it gives error as "
Key not found!! c
"

Can someone help.

The
csv
content is as below:

a,b,c
1,2,3
3,4,5

Answer

From the documentation, it seems the output should be [{"a": 1, "b": 2, "c": 3}, {"a": 3, "b": 4, "c": 5}], which is a JSON string.

To access the property of the objects, you would need to evaluate it with JSON.parse.

Another error could be in how you access c, but you didn't show the code for that.