Last active
September 23, 2022 13:54
-
-
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.
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
/*----------------------- | |
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