convert-csv-to-json
Advanced tools
Comparing version 0.0.3 to 0.0.4
29
index.js
'use strict'; | ||
/** | ||
* Parse .csv file and put its content into a file in json format. | ||
* @param {inputFileName} path/filename | ||
* @param {outputFileName} path/filename | ||
* | ||
*/ | ||
let csvToJson = require('./src/csvToJson.js'); | ||
exports.generateJsonFileFromCsv = function (inputFileName, outputFileName) { | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
}; | ||
exports.jsonToCsv= function(input,output){ | ||
csvToJson.generateJsonFileFromCsv(input, output); | ||
/** | ||
* Parse .csv file and put its content into an Array of Object in json format. | ||
* @param {inputFileName} path/filename | ||
* @return {Array} Array of Object in json format | ||
* | ||
*/ | ||
exports.getJsonFromCsv = function (inputFileName) { | ||
return csvToJson.getJsonFromCsv(inputFileName); | ||
}; | ||
/** | ||
* Parse .csv file and put its content into a file in json format. | ||
* @param {inputFileName} path/filename | ||
* @param {outputFileName} path/filename | ||
* | ||
* @deprecated Use generateJsonFileFromCsv() | ||
*/ | ||
exports.jsonToCsv = function (inputFileName, outputFileName) { | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
}; |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Convert CSV to JSON", | ||
@@ -16,3 +16,15 @@ "main": "index.js", | ||
"to", | ||
"json" | ||
"json", | ||
"csvtojson", | ||
"converter", | ||
"css", | ||
"json", | ||
"parser", | ||
"javascript", | ||
"nodejs", | ||
"node", | ||
"npm", | ||
"npm-package", | ||
"npm-module", | ||
"csv-parser" | ||
], | ||
@@ -19,0 +31,0 @@ "author": "iuccio", |
# CSVtoJSON | ||
Convert *csv* file to *JSON* file with Node.js. | ||
**This project is not dependent on others packages or libraries.** | ||
The cvs file must be **semicolon (;) separated**. | ||
Give an input file like: | ||
@@ -11,4 +15,12 @@ | ||
e.g. : | ||
```json | ||
first_name;last_name;email;gender;age | ||
Constantin;Langsdon;clangsdon0@hc360.com;Male;96 | ||
Norah;Raison;nraison1@wired.com;Female;32 | ||
``` | ||
will generate: | ||
```json | ||
@@ -34,26 +46,38 @@ [ | ||
## Prerequisites | ||
**npm**. [Installing npm](https://docs.npmjs.com/getting-started/installing-node). | ||
**NPM** (see [Installing Npm](https://docs.npmjs.com/getting-started/installing-node)). | ||
## npm | ||
npm package [convert-csv-to-json](https://www.npmjs.com/package/convert-csv-to-json). | ||
Go to NPM package [convert-csv-to-json](https://www.npmjs.com/package/convert-csv-to-json). | ||
### Install | ||
In terminal: | ||
```{r, engine='bash', count_lines} | ||
npm i convert-csv-to-json | ||
Install package in your *package.json* | ||
```bash | ||
$ npm install convert-csv-to-json --save | ||
``` | ||
Install package on your machine | ||
```bash | ||
$ npm install -g convert-csv-to-json | ||
``` | ||
### Usage | ||
#### Generate JSON file | ||
```js | ||
let csvToJson = require('convert-csv-to-json'); | ||
let fileInputName = './input.csv'; | ||
let fileOutputName = './output.json'; | ||
let fileInputName = './myInputFile.csv'; | ||
let fileOutputName = './myOutputFile.json'; | ||
csvToJson.jsonToCsv(fileInputName,fileOutputName); | ||
csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName); | ||
``` | ||
#### Generate Array of Object in JSON format | ||
```js | ||
let csvToJson = require('convert-csv-to-json'); | ||
let json = csvToJson.getJsonFromCsv("myInputFile.csv"); | ||
for(let i=0; i<json.length;i++){ | ||
console.log(json[i]); | ||
} | ||
``` | ||
## License | ||
CSVtoJSON is licensed under the GNU General Public License v3.0 [License](LICENSE). |
@@ -5,2 +5,3 @@ 'use strict'; | ||
let stringUtils = require('././util/stringUtils'); | ||
let jsonUtils = require('././util/jsonUtils'); | ||
@@ -13,12 +14,19 @@ const fieldDelimiter = ';'; | ||
generateJsonFileFromCsv(fileInputName, fileOutputName) { | ||
let parsedJson = this.getJsonFromCsv(fileInputName); | ||
fileUtils.writeFile(parsedJson, fileOutputName); | ||
let jsonStringified = this.getJsonFromCsvStringified(fileInputName); | ||
fileUtils.writeFile(jsonStringified, fileOutputName); | ||
} | ||
getJsonFromCsvStringified(fileInputName) { | ||
let json = this.getJsonFromCsv(fileInputName); | ||
let jsonStringified = JSON.stringify(json, undefined, 1); | ||
jsonUtils.validateJson(jsonStringified); | ||
return jsonStringified; | ||
} | ||
getJsonFromCsv(fileInputName) { | ||
let parsedCsv = fileUtils.readFile(fileInputName); | ||
return this.csvToJsonStringfy(parsedCsv); | ||
return this.csvToJson(parsedCsv); | ||
} | ||
csvToJsonStringfy(parsedCsv) { | ||
csvToJson(parsedCsv) { | ||
let lines = parsedCsv.split(newLine); | ||
@@ -38,6 +46,7 @@ let headers = lines[0].split(fieldDelimiter); | ||
} | ||
return JSON.stringify(jsonResult, undefined, 1); | ||
return jsonResult; | ||
} | ||
} | ||
module.exports = new CsvToJson(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49448
13
189
82