Created
December 16, 2013 06:01
-
-
Save leesei/7982923 to your computer and use it in GitHub Desktop.
#js #ya-csv #json2csv Convert JSON to CSV
This file contains hidden or 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
| #!/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