Skip to content

Instantly share code, notes, and snippets.

@jshaw
Last active September 14, 2016 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshaw/b18cfb0253f45339bd7e82630d2f519c to your computer and use it in GitHub Desktop.
Save jshaw/b18cfb0253f45339bd7e82630d2f519c to your computer and use it in GitHub Desktop.
JSON import + Export CSV for friend
var json2csv = require('json2csv');
var jsonfile = require('jsonfile');
var fs = require('fs');
var fields = ['Body', 'DateCreated', 'timestamp'];
var file = './jsontest_edit.json';
var import_json = "";
jsonfile.readFile(file, function(err, obj) {
console.dir(obj);
import_json = obj;
parsejson();
});
function parsejson(){
try {
var result = json2csv({flatten: true, data: import_json, fields: fields });
console.log(result);
saveCSV(result);
} catch (err) {
// Errors are thrown for bad options, or if the data is empty and no fields are provided.
// Be sure to provide fields if it is possible that your data array will be empty.
console.error(err);
}
}
function saveCSV(result){
console.log(result);
fs.writeFile('export.csv', result, function(err) {
if (err) throw err;
console.log('file saved');
});
}
// regex used:
// \"(-K.*?)\":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment