I'm new to node.js help me to solve this question.. In node.js it is showing like json2csv is not a function .. But i installed all packages of json2csv ... in node cmd "npm install json2csv" i did ..
var json2csv = require('json2csv');
var fs = require('fs');
var json = [
{
"car": "Audi",
"price": 40000,
"color": "blue"
}, {
"car": "BMW",
"price": 35000,
"color": "black"
}, {
"car": "Porsche",
"price": 60000,
"color": "green"
}
];
json2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) {
if (err) console.log(err);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
});
`
json2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) {
if (err) console.log(err);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
});
You can use this:
const json2csv = require('json2csv').parse;
const csv = json2csv(json, fields);
console.log(csv);
Have you referred the documentation https://github.com/zemirco/json2csv#json2csv-parser-synchronous-api about how to use the api ?
const json2csv = require('json2csv');
const csv = await json2csv.parse(json, fields);
console.log(csv);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With