https://github.com/limoer96/csv2json
A Tool convert CSV to JSON or JS Object with no dependencies.
https://github.com/limoer96/csv2json
csv json
Last synced: 12 months ago
JSON representation
A Tool convert CSV to JSON or JS Object with no dependencies.
- Host: GitHub
- URL: https://github.com/limoer96/csv2json
- Owner: Limoer96
- Created: 2019-07-24T12:34:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T03:32:28.000Z (about 3 years ago)
- Last Synced: 2025-03-20T18:08:58.067Z (about 1 year ago)
- Topics: csv, json
- Language: TypeScript
- Size: 1.69 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# csv2jsonfile
[](https://travis-ci.com/xiaomoer/csv2json)
> A Tool convert CSV to JSON or JS Object with NO Dependencies.
## Install
```bash
npm install --save csv2jsonfile
```
or
```bash
yarn add csv2jsonfile
```
see from [npm package](https://npmjs.org/package/csv2jsonfile).
## Usage
> note: the csv file must have header(used as key), like the example below:
```
city,level,alias
Shanghai,3,SH
Chongqing,3,SQ
Jinan,4,JN
```
### CSV to JSON
```javascript
const csv2jsonfile = require('csv2jsonfile');
const path = require('path');
const sourcePath = path.resolve(__dirname, 'data.csv');
const targetPath = path.resolve(__dirname, 'result.json');
// default convert CSV with header
csv2jsonfile(sourcePath, targetPath)
.then((time) => {
console.log(`use: ${time} ms`);
})
.catch((err) => {
console.log(err.message);
})
// result in result.json
/*
[
{
city: "Shangehai",
level: "3",
alias: "SH"
},
{
city: "Chongqing",
level: "3",
alias: "CQ"
},
]
*/
// CSV without header
csv2jsonfile(sourcePath, targetPath, { header: false })
.then((time) => {
console.log(`use: ${time} ms`);
})
.catch((err) => {
console.log(err.message);
})
```
### CSV to JavaScript Object
```javascript
var csv2jsonfile = require('csv2jsonfile');
csv2jsonfile.inline('data.csv')
.then((obj) => {
console.log(obj)
})
.catch((err) => {
console.log(err);
})
// CSV without header
csv2jsonfile.inline('data.csv', { header: false })
.then((obj) => {
console.log(obj);
})
.catch((err) => {
console.log(err);
})
```
## API
```javascript
/**
* convert csv to json file
* @param {string} sourcePath absolute path of csv file
* @param {string} targetPath absolute path of json file
* @param {object} options default { header: true }
* @return {Promise} ms usage through convert progress
*/
csv2jsonfile(sourcePath, targetPath, options?)
/**
* convert csv to javascript object
* @param {string} sourcePath absolute path of csv file
* @param {object} options default { header: true }
* @return {Promise} the object after convert
*/
csv2jsonfile.inline(sourcePath, options?)
```
## ChangeLog
### v1.2.0
1. Delete `targetPath` file if it exists before convert.(no need to manuallt clear)
2. add **No Header** CSV support.
If the CSV without header, It will be converted into **Array** each line.
E.g:
```javascript
// data.csv => data.json
// 1. without header
[
["Shanghai", "3", "SH"],
["Chongqing", "3", "CQ"],
["Jinan", "4", "JN"]
]
// with header
[
{
"city": "Shanghai",
"level": "3",
"alias": "SH"
},
{
"city": "Chongqing",
"level": "3",
"alias": "CQ"
},
{
"city": "Jinan",
"level": "4",
"alias": "JN"
}
]
```
3. fix bug: if there are no new empty line(`\n`) at the end of the file, may cause confusion.