csv2jsonfile
A Tool convert CSV to JSON or JS Object with NO Dependencies.
Install
npm install --save csv2jsonfile
or
yarn add csv2jsonfile
see from npm package.
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
const csv2jsonfile = ;const path = ;const sourcePath = path; const targetPath = path; // default convert CSV with header // result in result.json/*[ { city: "Shangehai", level: "3", alias: "SH" }, { city: "Chongqing", level: "3", alias: "CQ" },]*/// CSV without header
CSV to JavaScript Object
var csv2jsonfile = ;csv2jsonfile // CSV without headercsv2jsonfile
API
/** * convert csv to json file * @param * @param * @param * @return */
ChangeLog
v1.2.0
- Delete
targetPath
file if it exists before convert.(no need to manuallt clear) - add No Header CSV support.
If the CSV without header, It will be converted into Array each line. E.g:
// 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"
- fix bug: if there are no new empty line(
\n
) at the end of the file, may cause confusion.