Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Created September 1, 2022 20:08
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/6dc1b77c49a2713376911470b12e3161 to your computer and use it in GitHub Desktop.
Save celsowhite/6dc1b77c49a2713376911470b12e3161 to your computer and use it in GitHub Desktop.
Convert a csv file to json and save it to a folder.
/*-----------------------
Imports
-----------------------*/
import fs from "fs";
import glob from "glob";
import csv from "csvtojson";
/*-----------------------
Script
-----------------------*/
glob("./csv/*.csv", async function (er, files) {
files.forEach(async (file) => {
const fileName = file.replace(/^.*[\\\/]/, "").replace(".csv", "");
const data = await csv().fromFile(file);
const formattedData = data.reduce((a, v) => ({ ...a, [v.name]: v }), {});
fs.writeFileSync(`./json/${fileName}.json`, JSON.stringify(formattedData));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment