Created
May 16, 2022 13:02
-
-
Save mmaous/44a838bbe11195e0ad0ad70600731f16 to your computer and use it in GitHub Desktop.
Convert CSV data to JSON script
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
var csv = require('csv-parser') | |
var fs = require('fs') | |
var countries = [] | |
fs.createReadStream('./data.csv') | |
.pipe(csv(['label', 'value'])) | |
.on('data', function (data) { | |
if (data.label === 'label' && data.value === 'value') return | |
countries.push({ value: data.value, label: data.label }) | |
}) | |
.on('end', function () { | |
fs.writeFile('./data.json', JSON.stringify(countries, null, 2) + '\n', function (err) { | |
if (err) throw err | |
console.log('Wrote countries to data.json') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment