Created
February 16, 2026 14:43
-
-
Save mshivam019/ef16ba1eb8c7fc3e25d93d92de82dca8 to your computer and use it in GitHub Desktop.
This script is for converting csv to json
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
| const fs = require("fs"); | |
| const { parse } = require("csv-parse"); | |
| async function csvToJson(inputFile, outputFile) { | |
| return new Promise((resolve, reject) => { | |
| console.log(`Converting ${inputFile} to JSON...`); | |
| if (!fs.existsSync(inputFile)) { | |
| return reject(new Error(`Input file ${inputFile} does not exist`)); | |
| } | |
| const parser = parse({ | |
| columns: true, | |
| skip_empty_lines: true, | |
| trim: true, | |
| }); | |
| const input = fs.createReadStream(inputFile, { encoding: "utf-8" }); | |
| const output = fs.createWriteStream(outputFile); | |
| output.write("[\n"); // start JSON array | |
| let first = true; | |
| let count = 0; | |
| let preview = []; | |
| parser.on("data", (row) => { | |
| // Clean row | |
| const cleanedRow = {}; | |
| for (const key in row) { | |
| if (row[key]?.toString().trim()) { | |
| cleanedRow[key] = row[key]; | |
| } | |
| } | |
| if (!first) output.write(",\n"); | |
| output.write(JSON.stringify(cleanedRow, null, 2)); | |
| first = false; | |
| count++; | |
| // keep first 3 records for preview | |
| if (preview.length < 3) preview.push(cleanedRow); | |
| }); | |
| parser.on("end", () => { | |
| output.write("\n]\n"); // end JSON array | |
| output.end(); | |
| console.log(`Successfully parsed ${count} records`); | |
| console.log(`JSON saved to ${outputFile}`); | |
| console.log("\nPreview of first 3 records:"); | |
| preview.forEach((rec, i) => console.log(`Record ${i + 1}:`, rec)); | |
| resolve(); | |
| }); | |
| parser.on("error", (err) => reject(err)); | |
| input.pipe(parser); | |
| }); | |
| } | |
| // CLI usage | |
| if (require.main === module) { | |
| const args = process.argv.slice(2); | |
| if (args.length < 1) { | |
| console.log("Usage: node csv_to_json.js <input.csv> [output.json]"); | |
| console.log("Example: node csv_to_json.js data.csv converted.json"); | |
| process.exit(1); | |
| } | |
| const inputFile = args[0]; | |
| const outputFile = args[1] || inputFile.replace(/\.csv$/i, ".json"); | |
| csvToJson(inputFile, outputFile) | |
| .then(() => console.log("Conversion completed successfully!")) | |
| .catch((err) => { | |
| console.error("Conversion failed:", err.message); | |
| process.exit(1); | |
| }); | |
| } | |
| module.exports = { csvToJson }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment