Socket
Socket
Sign inDemoInstall

json-compressor-for-csv

Package Overview
Dependencies
6
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-compressor-for-csv

Module compress JSON to be easily exported to CSV


Version published
Weekly downloads
4
Maintainers
1
Install size
3.35 MB
Created
Weekly downloads
 

Readme

Source

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

FAQs

Last updated on 28 Nov 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc