Skip to content

Instantly share code, notes, and snippets.

@williscool
Last active June 6, 2017 05:43
Show Gist options
  • Save williscool/2f9f6446499c69a215fd85362aae8e7d to your computer and use it in GitHub Desktop.
Save williscool/2f9f6446499c69a215fd85362aae8e7d to your computer and use it in GitHub Desktop.

Revisions

  1. williscool revised this gist Jun 6, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions journey-note-to-csv.js
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,11 @@ glob("*.json", options, function (er, files) {

    let jsoned = files.map((f) => JSON.parse(fs.readFileSync(f, 'utf8')));

    let fields = ['date_journal','address', 'text'];
    jsoned.forEach((o) => o.date_time = new Date(o.date_journal));

    let fields = ['date_time','address', 'text'];
    let fieldNames = ['Date','Address', 'Note'];
    let csv = json2csv({ data: jsoned, fields: fields, fieldNames: fieldNames });

    fs.writeFileSync('output.csv', csv);
    })
    });
  2. williscool renamed this gist Jun 6, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. williscool created this gist Jun 6, 2017.
    22 changes: 22 additions & 0 deletions journey-to-csv.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    const fs = require('fs');
    const glob = require('glob');
    const json2csv = require('json2csv');

    const options = {};

    glob("*.json", options, function (er, files) {
    // files is an array of filenames.
    // If the `nonull` option is set, and nothing
    // was found, then files is ["**/*.js"]
    // er is an error object or null.

    console.log(`Num of files: ${files.length}`);

    let jsoned = files.map((f) => JSON.parse(fs.readFileSync(f, 'utf8')));

    let fields = ['date_journal','address', 'text'];
    let fieldNames = ['Date','Address', 'Note'];
    let csv = json2csv({ data: jsoned, fields: fields, fieldNames: fieldNames });

    fs.writeFileSync('output.csv', csv);
    })