Skip to content

Instantly share code, notes, and snippets.

@mmaous
Created May 16, 2022 13:02
Show Gist options
  • Save mmaous/44a838bbe11195e0ad0ad70600731f16 to your computer and use it in GitHub Desktop.
Save mmaous/44a838bbe11195e0ad0ad70600731f16 to your computer and use it in GitHub Desktop.
Convert CSV data to JSON script
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