Skip to content

Instantly share code, notes, and snippets.

@leesei
Created December 16, 2013 06:01
Show Gist options
  • Save leesei/7982923 to your computer and use it in GitHub Desktop.
Save leesei/7982923 to your computer and use it in GitHub Desktop.
#js #ya-csv #json2csv Convert JSON to CSV
#!/usr/bin/env node
var csv = require('ya-csv');
var path = require('path');
// load the json file
var json = require(path.resolve(process.argv[2]));
console.log(json);
console.log('');
var writer = csv.createCsvStreamWriter(process.stdout);
var keys = Object.keys(json[0]);
// write the header
writer.writeRecord(keys);
json.forEach(function (item, index) {
// create an array for the record
var record = [];
keys.forEach(function (key) {
record.push(item[key]);
});
writer.writeRecord(record);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment