json-compressor-for-csv
v1.9.8
Published
Module compress JSON to be easily exported to CSV
Downloads
8
Readme
Installation
npm install json-compressor-for-csv
Usage
Step 1: Initialization
const compressor = require('json-compressor-for-csv');
compressor.compress({
list: _uncompressed, // ARRAY only
splitter : "-" // default "-"
}, function (jsv) {
console.log("Compressed " + JSON.stringify(jsv, 0, 2));
});
Exemples
Exemple 1
let _uncompressed = [{
"firstName": "andrea",
"lastName": "giglio",
"games": [
{
"id": 0,
"code": "A"
}]
}];
Compressed
{
"partials": [
{
"firstName": "andrea",
"lastName": "giglio",
"games-id": 0,
"games-code": "A"
}
],
"allKeys": [
"firstName",
"lastName",
"games-id",
"games-code"
]
}
Exemple 2
let _uncompressed = [
{
"firstName": "luca",
"lastName": "medici",
"games": [
{
"id": 0,
"code": "H",
"actions": [
{
"id": 0,
"code": "I"
}]
}]
},{
"firstName": "andrea",
"lastName": "giglio",
"games": [
{
"id": 0,
"code": "A"
},
{
"id": 1,
"code": "B",
"numer": 7
},
{
"id": 2,
"code": "C",
"actions": [
{
"id": 0,
"code": "I",
"numer": 8
}]
}]
}];
Compressed
{
"partials": [
{
"firstName": "luca",
"lastName": "medici",
"games-id": 0,
"games-code": "H",
"games-actions-id": 0,
"games-actions-code": "I"
},
{
"firstName": "andrea",
"lastName": "giglio",
"games-id": 0,
"games-code": "A"
},
{
"firstName": "andrea",
"lastName": "giglio",
"games-id": 1,
"games-code": "B",
"games-numer": 7
},
{
"firstName": "andrea",
"lastName": "giglio",
"games-id": 2,
"games-code": "C",
"games-actions-id": 0,
"games-actions-code": "I",
"games-actions-numer": 8
}
],
"allKeys": [
"firstName",
"lastName",
"games-id",
"games-code",
"games-actions-id",
"games-actions-code",
"games-numer",
"games-actions-numer"
]
}
Export to CSV
You can convert the compressed json using json2csv
npm install json-compressor-for-csv
npm install json2csv
const compressor = require('json-compressor-for-csv'),
json2csv = require('json2csv');
compressor.compress({
list: _uncompressed
}, function (jsv) {
//console.log("COMPRESSED", jsv);
json2csv({
data: JSON.parse(JSON.stringify(jsv.partials)),
fields: jsv.allKeys,
del: ';'
},
function (err, csv) {
//console.log("CSV", csv);
});
});
Tests
> node test/test.js
TODO - BUGS
> Refactory
> Fix Bug for 2nd level nested array
> @ DELIMITER will be set generic value