3

I have the following code which works well, but I'd like this to save and write to a CSV file in the folder I'm in. I'm running the JS in node. Big thanks!

var jsonexport = require('jsonexport');

json = [ { uniq_id: [ 'test' ],
    product_url: [ 'http://www.here.com' ],
    manufacturer: [ 'Disney' ]}]

jsonexport(json,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
    });

Note: jsonexport is a JSON-to-CSV converter.

2
  • Have you made sure your JSON is valid? I see a trailing comma at the end of the last array. Commented Apr 29, 2015 at 20:46
  • yea sorry, just a copy paste error to make it simpler looking Commented Apr 29, 2015 at 21:15

1 Answer 1

4

UPDATE you can Use something like this.

jsonexport(json,function(err, csv){
   fs.writeFile("/tmp/test.csv", csv, function(err) {
   if(err) {}
   });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm running this in node so there's no 'window'
you can Use fs.writeFile("/tmp/test", csv, function(err) { if(err) {} }); I Updated the Answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.