Node.js csv파일을 json파일로 변환하기

jieun·2023년 1월 2일
0

nodejs

목록 보기
2/2

1. 세팅

  • (.gitignore 파일 생성)
  • assets 폴더, 하위 폴더 생성
  • 변환할 csv 파일 임포트
  • csvtojson 모듈 설치
$npm i csvtojson --save

2. index.js 생성

  • folerName, fileName 변경
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
profile
개발새발 블로그

0개의 댓글