user2848031 user2848031 - 17 days ago 11
Javascript Question

How to export csv file from json array using nodejs?

How to export json array to csv file in node js ? Below is format of json data which need to convert to csv file. Please advise.

I want to display only three column of values from json data. Column A : total number of response , Column B : Name , Column C : field1

Here is my code :

var json2csv = require('json2csv');
var fields = ['total', 'results.name.val'];
var csv = json2csv({ data: data, fields: fields });

fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});


Using this code i got total but name column coming as empty.
Columns name are:
total , name , field1

{"total":1996,
"results":
[
{
"name":
{
"val":"test value1",
"id":"0271b276",
"type":"String",
},
"comments":[],
"attributes":
[
{

**"val":"test value2 ",
"type":"field1",**

},
{

"val":"test description",
"type":"field2",

},
{

"val":"test123",
"type":"field3",

}
],
"type":
{
"val":"Business1",
"type":"string",

},
"context":
{
"val":"contecxt1",
"type":"string",

},
"status":"Accepted",
"parents":[]
}]}

Answer
results[0].name.val
results[1].name.val
results[2].name.val

You need to do the rest here is some further hint