convert-json
Advanced tools
Comparing version 0.3.3 to 0.4.0
var fs = require('fs'); | ||
var csv_json = require('node-csv-json'); | ||
var xls_json = require('xls-to-json'); | ||
var xlsx_json = require('xlsx-to-json'); | ||
var xml_json = require('xml-to-json'); | ||
var tsv_json = require('node-tsv-json'); | ||
var csv = require('csv-streamify'); | ||
var XLS = require('xlsjs'); | ||
var XLSX = require('xlsx'); | ||
var xml2js = require('xml2js'); | ||
module.exports = CV_json; | ||
function csv_json (file, opts, cb) { | ||
var fstream = fs.createReadStream(file); | ||
if(arguments.length === 2 && typeof arguments[1] === 'function') { | ||
// no opts | ||
cb = opts; | ||
opts = null; | ||
} | ||
function CV_json (config, callback) { | ||
return fstream.pipe(csv(opts, cb)); | ||
} | ||
if(!config.input) { | ||
console.error("You have to have an input file"); | ||
process.exit(1); | ||
function xls_json (file, opts, cb) { | ||
if(arguments.length === 2 && typeof arguments[1] === 'function') { | ||
// no opts | ||
cb = opts; | ||
opts = null; | ||
} | ||
var cv = new _CV_json(config, callback); | ||
return cb(null, XLS.readFile(file, opts)); | ||
} | ||
_CV_json = function(config, callback) { | ||
var exten = this._getExtension(config.input).toLowerCase(); | ||
switch(exten) { | ||
case 'csv': | ||
return this.cvCSV(config, callback); | ||
break; | ||
case 'tsv': | ||
return this.cvTSV(config, callback); | ||
break; | ||
case 'xls': | ||
return this.cvXLS(config, callback); | ||
break; | ||
case 'xlsx': | ||
return this.cvXLSX(config, callback); | ||
break; | ||
case 'xml': | ||
return this.cvXML(config, callback); | ||
default: | ||
if(config.type==='csv' && config.input) | ||
return this.cvCSV(config, callback); | ||
else | ||
return callback('Not Support'); | ||
function xlsx_json (file, opts, cb) { | ||
if(arguments.length === 2 && typeof arguments[1] === 'function') { | ||
// no opts | ||
cb = opts; | ||
opts = null; | ||
} | ||
} | ||
_CV_json.prototype._getExtension = function(filename) { | ||
return filename.split('.').pop(); | ||
return cb(null, XLSX.readFile(file, opts)); | ||
} | ||
_CV_json.prototype.cvCSV = function(config, callback) { | ||
csv_json({ | ||
input: config.input, | ||
output: config.output | ||
}, function(err, result){ | ||
if(err) { | ||
callback(err) | ||
}else { | ||
callback(null, result) | ||
} | ||
}); | ||
function xml_json (file, opts, cb) { | ||
if(arguments.length === 2 && typeof arguments[1] === 'function') { | ||
// no opts | ||
cb = opts; | ||
opts = null; | ||
} | ||
} | ||
_CV_json.prototype.cvTSV = function(config, callback) { | ||
tsv_json({ | ||
input: config.input, | ||
output: config.output | ||
}, function(err, result){ | ||
if(err) { | ||
callback(err) | ||
}else { | ||
callback(null, result) | ||
} | ||
var parser = new xml2js.Parser(); | ||
fs.readFile(file, function(err, data) { | ||
if(err) | ||
cb(err); | ||
else | ||
parser.parseString(data, cb); | ||
}); | ||
} | ||
_CV_json.prototype.cvXLS = function(config, callback) { | ||
xls_json({ | ||
input: config.input, | ||
output: config.output | ||
}, function(err, result){ | ||
if(err) { | ||
callback(err) | ||
}else { | ||
callback(null, result) | ||
} | ||
}); | ||
} | ||
_CV_json.prototype.cvXLSX = function(config, callback) { | ||
xlsx_json({ | ||
input: config.input, | ||
output: config.output | ||
}, function(err, result){ | ||
if(err) { | ||
callback(err) | ||
}else { | ||
callback(null, result) | ||
} | ||
}); | ||
} | ||
_CV_json.prototype.cvXML = function(config, callback) { | ||
xml_json({ | ||
input: config.input, | ||
output: config.output | ||
}, function(err, result){ | ||
if(err) { | ||
callback(err) | ||
}else { | ||
callback(null, result) | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
csv: csv_json, | ||
xls: xls_json, | ||
xlsx: xlsx_json, | ||
xml: xml_json | ||
}; |
{ | ||
"name": "convert-json", | ||
"version": "0.3.3", | ||
"description": "auto detect file type and convert it to json format", | ||
"version": "0.4.0", | ||
"description": "A collection of converting multiple formats of files to JSON.", | ||
"main": "index.js", | ||
@@ -27,7 +27,6 @@ "scripts": { | ||
"dependencies": { | ||
"xls-to-json": "0.2.2", | ||
"xlsx-to-json": "0.2.3", | ||
"node-csv-json": "0.2.1", | ||
"xml-to-json": "0.1.1", | ||
"node-tsv-json": "~0.2.0" | ||
"csv-streamify": "^1.0.0", | ||
"xlsjs": "^0.7.0", | ||
"xlsx": "^0.7.7", | ||
"xml2js": "^0.4.4" | ||
}, | ||
@@ -34,0 +33,0 @@ "devDependencies": { |
121
README.md
# Convert JSON | ||
[![Build Status](https://travis-ci.org/DataGarage/convert-json.png?branch=master)](https://travis-ci.org/DataGarage/convert-json) | ||
A collection of converting multiple formats of files to JSON. | ||
Auto detect different type of file format and convert it into JSON. | ||
## Support | ||
- csv | ||
- xlsx | ||
- xls | ||
- xml | ||
- tsv | ||
- csv, tsv, dsv.. (using `stream`) | ||
- xlsx (using `readFile`) | ||
- xls (using `readFile`) | ||
- xml (using `readFile`) | ||
@@ -20,45 +17,89 @@ ## Install | ||
``` | ||
## Usage | ||
## Sample | ||
### CSV | ||
```javascript | ||
var cv_json = require('..'); | ||
var cv2json = require('../'); | ||
cv_json({ | ||
// now supporting csv, xls, xlsx, tsv, xml format | ||
input: __dirname + '/number_format.xls', | ||
output: null | ||
}, function(err, result) { | ||
if(err) { | ||
console.error(err); | ||
}else { | ||
console.log(result); | ||
} | ||
} | ||
); | ||
var csv_trans = cv2json.csv('./test/test.csv', options /* optional */, function(err, result) { | ||
if(err) | ||
console.error(err); | ||
else | ||
console.log(result); | ||
// result should be a json object or array. | ||
}) | ||
``` | ||
In config object, you have to enter an input path. But If you don't want to output any file you can set to `null`. | ||
The first argument is a csv input file, for options see details https://github.com/klaemo/csv-stream#options, and the last argument is a callback function. | ||
#### input string to csv | ||
if you input string istead a file path, bring a `type` parameter | ||
### XLS | ||
```javascript | ||
cv_json({ | ||
type: 'csv', | ||
input: "<csv string...>", | ||
output: null | ||
}, function(err, result) { | ||
if(err) { | ||
console.error(err); | ||
}else { | ||
console.log(result); | ||
} | ||
} | ||
); | ||
var cv2json = require('../'); | ||
var csv_trans = cv2json.xls('./test/test.xls', options /* optional */, function(err, result) { | ||
if(err) | ||
console.error(err); | ||
else | ||
console.log(result); | ||
// result should be a json object or array. | ||
}) | ||
``` | ||
The first argument is a xls input file, for options see details https://github.com/SheetJS/js-xls#parsing-options, and the last argument is a callback function. | ||
### XLSX | ||
```javascript | ||
var cv2json = require('../'); | ||
var csv_trans = cv2json.xlsx('./test/test.xlsx', options /* optional */, function(err, result) { | ||
if(err) | ||
console.error(err); | ||
else | ||
console.log(result); | ||
// result should be a json object or array. | ||
}) | ||
``` | ||
The first argument is a xlsx input file, for options see details https://github.com/SheetJS/js-xlsx#parsing-options, and the last argument is a callback function. | ||
### XML | ||
```javascript | ||
var cv2json = require('../'); | ||
var csv_trans = cv2json.xml('./test/test.xml', options /* optional */, function(err, result) { | ||
if(err) | ||
console.error(err); | ||
else | ||
console.log(result); | ||
// result should be a json object or array. | ||
}) | ||
``` | ||
The first argument is a xls input file, for options see details https://github.com/Leonidas-from-XIV/node-xml2js#options, and the last argument is a callback function. | ||
## Library using | ||
- csv | ||
https://github.com/klaemo/csv-stream | ||
- xlsx | ||
https://github.com/SheetJS/js-xlsx | ||
- xls | ||
https://github.com/SheetJS/js-xls | ||
- xml | ||
https://github.com/Leonidas-from-XIV/node-xml2js | ||
## License | ||
MIT [@chiljung](http://github.com/chilijung) | ||
MIT |
<<<<<<< HEAD | ||
[{"":"","Format":"0","Valuie":"12345.6789","B Fmt":"General","VBA Fmt":"","Fmt":"","Macro":""},{"":"","Format":"1","Valuie":"12346","B Fmt":"0","VBA Fmt":"12346","Fmt":"0","Macro":"12346"},{"":"","Format":"2","Valuie":"12345.68","B Fmt":"0.00","VBA Fmt":"12345.68","Fmt":"0.00","Macro":"12345.68"},{"":"","Format":"3","Valuie":"12","B Fmt":"346","VBA Fmt":"#","Fmt":"##0","Macro":"12"},{"":"","Format":"4","Valuie":"12","B Fmt":"345.68","VBA Fmt":"#","Fmt":"##0.00","Macro":"12"},{"":"","Format":"9","Valuie":"1234568%","B Fmt":"0%","VBA Fmt":"1234568%","Fmt":"0%","Macro":"1234568%"},{"":"","Format":"10","Valuie":"1234567.89%","B Fmt":"0.00%","VBA Fmt":"1234567.89%","Fmt":"0.00%","Macro":"1234567.89%"},{"":"","Format":"11","Valuie":"1.23E+04","B Fmt":"0.00E+00","VBA Fmt":"1.23E+04","Fmt":"0.00E+00","Macro":"1.23E+04"},{"":"","Format":"12","Valuie":"12345 2/3","B Fmt":"# ?/?","VBA Fmt":"12346 ?/?","Fmt":"# ?/?","Macro":"12345 2/3"},{"":"","Format":"13","Valuie":"12345 55/81","B Fmt":"# ??/??","VBA Fmt":"12346 ??/??","Fmt":"# ??/??","Macro":"12345 55/81"},{"":"* What is this supposed to be?","Format":"14","Valuie":"10/18/33","B Fmt":"m/d/yy","VBA Fmt":"10/18/33","Fmt":"m/d/yy","Macro":"10/18/33"},{"":"","Format":"15","Valuie":"18-Oct-33","B Fmt":"d-mmm-yy","VBA Fmt":"18-Oct-33","Fmt":"d-mmm-yy","Macro":"18-Oct-33"},{"":"","Format":"16","Valuie":"18-Oct","B Fmt":"d-mmm","VBA Fmt":"18-Oct","Fmt":"d-mmm","Macro":"18-Oct"},{"":"","Format":"17","Valuie":"Oct-33","B Fmt":"mmm-yy","VBA Fmt":"Oct-33","Fmt":"mmm-yy","Macro":"Oct-33"},{"":"","Format":"18","Valuie":"4:17 PM","B Fmt":"h:mm AM/PM","VBA Fmt":"4:17 PM","Fmt":"h:mm AM/PM","Macro":"4:17 PM"},{"":"","Format":"19","Valuie":"4:17:37 PM","B Fmt":"h:mm:ss AM/PM","VBA Fmt":"4:17:37 PM","Fmt":"h:mm:ss AM/PM","Macro":"4:17:37 PM"},{"":"","Format":"20","Valuie":"16:17","B Fmt":"h:mm","VBA Fmt":"16:17","Fmt":"h:mm","Macro":"16:17"},{"":"","Format":"21","Valuie":"16:17:37","B Fmt":"h:mm:ss","VBA Fmt":"16:17:37","Fmt":"h:mm:ss","Macro":"16:17:37"},{"":"","Format":"22","Valuie":"10/18/33 16:17","B Fmt":"m/d/yy h:mm","VBA Fmt":"10/18/33 16:17","Fmt":"m/d/yy h:mm","Macro":"10/18/33 16:17"},{"":"","Format":"37","Valuie":"12","B Fmt":"346","VBA Fmt":"#","Fmt":"##0 ;(#","Macro":"##0)"},{"":"","Format":"38","Valuie":"12","B Fmt":"346","VBA Fmt":"#","Fmt":"##0 ;[Red](#","Macro":"##0)"},{"":"","Format":"39","Valuie":"12","B Fmt":"345.68","VBA Fmt":"#","Fmt":"##0.00;(#","Macro":"##0.00)"},{"":"","Format":"40","Valuie":"12","B Fmt":"345.68","VBA Fmt":"#","Fmt":"##0.00;[Red](#","Macro":"##0.00)"},{"":"","Format":"45","Valuie":"17:37","B Fmt":"mm:ss","VBA Fmt":"10:37","Fmt":"mm:ss","Macro":"17:37"},{"":"","Format":"46","Valuie":"296296:17:37","B Fmt":"[h]:mm:ss","VBA Fmt":":10:37","Fmt":"[h]:mm:ss","Macro":"296296:17:37"},{"":"","Format":"47","Valuie":"1737.0","B Fmt":"mmss.0","VBA Fmt":"1037.0","Fmt":"mmss.0","Macro":"1737.0"},{"":"","Format":"48","Valuie":"12.3E+3","B Fmt":"##0.0E+0","VBA Fmt":"123.5E+2","Fmt":"##0.0E+0","Macro":"12.3E+3"},{"":"","Format":"49","Valuie":"12345.6789","B Fmt":"@","VBA Fmt":"12345.6789","Fmt":"@","Macro":"12345.6789"}] | ||
======= | ||
<<<<<<< HEAD | ||
[{"":"","Format":"0","Valuie":"12345.6789","B Fmt":"General","VBA Fmt":"","Fmt":"","Macro":""},{"":"","Format":"1","Valuie":"12346","B Fmt":"0","VBA Fmt":"12346","Fmt":"0","Macro":"12346"},{"":"","Format":"2","Valuie":"12345.68","B Fmt":"0.00","VBA Fmt":"12345.68","Fmt":"0.00","Macro":"12345.68"},{"":"","Format":"3","Valuie":"12,346","B Fmt":"#,##0","VBA Fmt":"12,346","Fmt":"#,##0","Macro":"12,346"},{"":"","Format":"4","Valuie":"12,345.68","B Fmt":"#,##0.00","VBA Fmt":"12,345.68","Fmt":"#,##0.00","Macro":"12,345.68"},{"":"","Format":"9","Valuie":"1234568%","B Fmt":"0%","VBA Fmt":"1234568%","Fmt":"0%","Macro":"1234568%"},{"":"","Format":"10","Valuie":"1234567.89%","B Fmt":"0.00%","VBA Fmt":"1234567.89%","Fmt":"0.00%","Macro":"1234567.89%"},{"":"","Format":"11","Valuie":"1.23E+04","B Fmt":"0.00E+00","VBA Fmt":"1.23E+04","Fmt":"0.00E+00","Macro":"1.23E+04"},{"":"","Format":"12","Valuie":"12345 2/3","B Fmt":"# ?/?","VBA Fmt":"12346 ?/?","Fmt":"# ?/?","Macro":"12345 2/3"},{"":"","Format":"13","Valuie":"12345 55/81","B Fmt":"# ??/??","VBA Fmt":"12346 ??/??","Fmt":"# ??/??","Macro":"12345 55/81"},{"":"* What is this supposed to be?","Format":"14","Valuie":"10/18/33","B Fmt":"m/d/yy","VBA Fmt":"10/18/33","Fmt":"m/d/yy","Macro":"10/18/33"},{"":"","Format":"15","Valuie":"18-Oct-33","B Fmt":"d-mmm-yy","VBA Fmt":"18-Oct-33","Fmt":"d-mmm-yy","Macro":"18-Oct-33"},{"":"","Format":"16","Valuie":"18-Oct","B Fmt":"d-mmm","VBA Fmt":"18-Oct","Fmt":"d-mmm","Macro":"18-Oct"},{"":"","Format":"17","Valuie":"Oct-33","B Fmt":"mmm-yy","VBA Fmt":"Oct-33","Fmt":"mmm-yy","Macro":"Oct-33"},{"":"","Format":"18","Valuie":"4:17 PM","B Fmt":"h:mm AM/PM","VBA Fmt":"4:17 PM","Fmt":"h:mm AM/PM","Macro":"4:17 PM"},{"":"","Format":"19","Valuie":"4:17:37 PM","B Fmt":"h:mm:ss AM/PM","VBA Fmt":"4:17:37 PM","Fmt":"h:mm:ss AM/PM","Macro":"4:17:37 PM"},{"":"","Format":"20","Valuie":"16:17","B Fmt":"h:mm","VBA Fmt":"16:17","Fmt":"h:mm","Macro":"16:17"},{"":"","Format":"21","Valuie":"16:17:37","B Fmt":"h:mm:ss","VBA Fmt":"16:17:37","Fmt":"h:mm:ss","Macro":"16:17:37"},{"":"","Format":"22","Valuie":"10/18/33 16:17","B Fmt":"m/d/yy h:mm","VBA Fmt":"10/18/33 16:17","Fmt":"m/d/yy h:mm","Macro":"10/18/33 16:17"},{"":"","Format":"37","Valuie":"12,346","B Fmt":"#,##0 ;(#,##0)","VBA Fmt":"12,346","Fmt":"#,##0 ;(#,##0)","Macro":"12,346"},{"":"","Format":"38","Valuie":"12,346","B Fmt":"#,##0 ;[Red](#,##0)","VBA Fmt":"12,346","Fmt":"#,##0 ;[Red](#,##0)","Macro":"12,346"},{"":"","Format":"39","Valuie":"12,345.68","B Fmt":"#,##0.00;(#,##0.00)","VBA Fmt":"12,345.68","Fmt":"#,##0.00;(#,##0.00)","Macro":"12,345.68"},{"":"","Format":"40","Valuie":"12,345.68","B Fmt":"#,##0.00;[Red](#,##0.00)","VBA Fmt":"12,345.68","Fmt":"#,##0.00;[Red](#,##0.00)","Macro":"12,345.68"},{"":"","Format":"45","Valuie":"17:37","B Fmt":"mm:ss","VBA Fmt":"10:37","Fmt":"mm:ss","Macro":"17:37"},{"":"","Format":"46","Valuie":"296296:17:37","B Fmt":"[h]:mm:ss","VBA Fmt":":10:37","Fmt":"[h]:mm:ss","Macro":"296296:17:37"},{"":"","Format":"47","Valuie":"1737.0","B Fmt":"mmss.0","VBA Fmt":"1037.0","Fmt":"mmss.0","Macro":"1737.0"},{"":"","Format":"48","Valuie":"12.3E+3","B Fmt":"##0.0E+0","VBA Fmt":"123.5E+2","Fmt":"##0.0E+0","Macro":"12.3E+3"},{"":"","Format":"49","Valuie":"12345.6789","B Fmt":"@","VBA Fmt":"12345.6789","Fmt":"@","Macro":"12345.6789"}] | ||
@@ -6,1 +9,2 @@ ======= | ||
>>>>>>> 399a2af42897c9bc5297d9f8f7d925a24476b275 | ||
>>>>>>> 24ab53f3a2dd64a0e7b40eff896899656bbe7c88 |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
300923
4
22
105
1
182
1
+ Addedcsv-streamify@^1.0.0
+ Addedxlsjs@^0.7.0
+ Addedxlsx@^0.7.7
+ Addedxml2js@^0.4.4
+ Addedbuffer-from@1.1.2(transitive)
+ Addedcfb@0.11.1(transitive)
+ Addedcodepage@1.3.8(transitive)
+ Addedcolors@0.6.2(transitive)
+ Addedconcat-stream@2.0.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcsv-streamify@1.0.0(transitive)
+ Addedexit-on-epipe@1.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjszip@2.4.0(transitive)
+ Addedpako@0.2.9(transitive)
+ Addedreadable-stream@1.1.143.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedssf@0.8.2(transitive)
+ Addedstring_decoder@0.10.311.3.0(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxlsjs@0.7.6(transitive)
+ Addedxlsx@0.7.12(transitive)
- Removednode-csv-json@0.2.1
- Removednode-tsv-json@~0.2.0
- Removedxls-to-json@0.2.2
- Removedxlsx-to-json@0.2.3
- Removedxml-to-json@0.1.1
- Removedbyline@4.1.1(transitive)
- Removedcfb@1.2.2(transitive)
- Removedcolors@1.4.0(transitive)
- Removedcsv@0.3.7(transitive)
- Removednode-csv-json@0.2.1(transitive)
- Removednode-tsv-json@0.2.1(transitive)
- Removedssf@0.7.1(transitive)
- Removedxls-to-json@0.2.2(transitive)
- Removedxlsjs@0.6.20(transitive)
- Removedxlsx-to-json@0.2.3(transitive)
- Removedxml-to-json@0.1.1(transitive)