How to convert json file to excel using node.js given 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');
  });
});
      

Run codeHide result


 i wrote some code to convert json to csv but it doesn't work, can someone help me. This will be of great help [for me
+3


source to share


1 answer


It looks like you are using a named variable csv

, but the name of your function parameter excel

. It should be:

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

      



instead

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

      

+1


source







All Articles