Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Last active September 23, 2022 13:54
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 celsowhite/fb25ed6d2ddd10fb4f2ab3f3cfbd8c65 to your computer and use it in GitHub Desktop.
Save celsowhite/fb25ed6d2ddd10fb4f2ab3f3cfbd8c65 to your computer and use it in GitHub Desktop.
Given a json file or json data output it's contents to a csv.
/*-----------------------
Imports
-----------------------*/
import fs from "fs";
import csv from "csvtojson";
import path from "path";
import { fileURLToPath } from "url";
import { stringify } from "csv";
/*-----------------------
Script
-----------------------*/
// ES6 method to get relative file.
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Paths
const jsonInput = path.join(__dirname, "../input", "data.json");
const csvOutput = path.join(__dirname, "../output", "data.csv");
// Alter data
// Normally perform some operations here to alter the json data and format it.
const data = jsonInput.map((item) => item);
// Output
stringify(data, { header: true }, function (err, output) {
fs.writeFileSync(csvOutPut, output);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment