Last active
September 14, 2016 22:09
-
-
Save jshaw/b18cfb0253f45339bd7e82630d2f519c to your computer and use it in GitHub Desktop.
JSON import + Export CSV for friend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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