Created
September 1, 2022 20:08
-
-
Save celsowhite/6dc1b77c49a2713376911470b12e3161 to your computer and use it in GitHub Desktop.
Convert a csv file to json and save it to a folder.
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 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