1. 세팅
- (.gitignore 파일 생성)
- assets 폴더, 하위 폴더 생성
- 변환할 csv 파일 임포트
- csvtojson 모듈 설치
$npm i csvtojson --save
2. index.js 생성
const CSVToJSON = require('csvtojson')
const fs = require('fs');
const folderName = 'TEST'
const fileName = 'test'
CSVToJSON()
// csv convert to json array
.fromFile(`assets/${folderName}/${fileName}.csv`)
.then(data => {
// console.log(data)
// save json file
fs.writeFile(`assets/${folderName}/${fileName}.json`, JSON.stringify(data, null, 4), err => {
if (err) {
throw err
}
console.log('JSON array is saved.')
})
})
.catch(err => {
console.log(err)
})
3. 실행
$node index.js