Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert the json file into excel using node.js considering twitter data in mongodb

var json2csv = require('json2csv');
    
json2csv({data: someJSONData, fields: ['screen_name', 'statuses_count', 'location']}, function(err,csv) {
  if (err) console.log(err);
  console.log(csv);
});
var json = [
  {
    "screen_name: "Doug_E_Stile"",
    "statuses_count": 1546,
    "location": "Maryland""
  }, {
    "screen_name": "cthxidm",,
    "statuses_count": 339,
    "location": "peru manyas¿",
  }, {
    "screen_name": ""jiiiiimarjoh",
    "statuses_count":  4205,,
    "location": "South Cotabato, Philippines"
  }
];
json2csv({data: json, fields: ['screen_name', 'statuses_count', 'location']}, function(err, excel) {
  if (err) console.log(err);
  fs.writeFile('file.csv', csv, function(err) {
    if (err) throw err;
    console.log('file saved');
  });
});
i wrote the code to convert the json into the csv but it not working me can anybody help me .it will be great help[ for me
like image 890
MISY Avatar asked Jan 22 '26 06:01

MISY


1 Answers

Looks like you are using a variable named csv, but the name of your function parameter is excel. It should be:

fs.writeFile('file.csv', excel, function(err) {

instead of

fs.writeFile('file.csv', csv, function(err) {
like image 59
Kai Sternad Avatar answered Jan 23 '26 19:01

Kai Sternad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!