# compressed-json

> String base JSON compressor

Latest version **1.0.16** (published 2021-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install compressed-json
pnpm add compressed-json
yarn add compressed-json
bun add compressed-json
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.16 |
| Published | 2021-01-06 |
| First published | 2019-07-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 62.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 49 |
| Author | Taka Okunish |
| Maintainers | okunishinishi |
| Keywords | JSON |

## Links

- npm: https://www.npmjs.com/package/compressed-json
- Repository: https://github.com/okunishinishi/node-compressed-json
- Homepage: https://github.com/okunishinishi/node-compressed-json#readme
- Issues: https://github.com/okunishinishi/node-compressed-json/issues
- npm.io page: https://npm.io/package/compressed-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.16 (latest) — 2021-01-06
- 1.0.15 — 2019-09-09
- 1.0.14 — 2019-07-23
- 1.0.13 — 2019-07-23
- 1.0.12 — 2019-07-23
- 1.0.11 — 2019-07-23
- 1.0.10 — 2019-07-23
- 1.0.9 — 2019-07-23
- 1.0.8 — 2019-07-23
- 1.0.7 — 2019-07-23
- 1.0.6 — 2019-07-22
- 1.0.5 — 2019-07-22
- 1.0.4 — 2019-07-22
- 1.0.3 — 2019-07-22
- 1.0.2 — 2019-07-22
- … 2 more at https://npm.io/package/compressed-json/versions

## README

# compressed-json

[bd_npm_shield_url]: http://img.shields.io/npm/v/install-if-needed.svg?style=flat
[bd_npm_url]: http://www.npmjs.org/package/compressed-json

[![npm Version][bd_npm_shield_url]][bd_npm_url]

String base JSON compressor

If you seriously want to reduce your data size, consider using [msgpack](https://msgpack.org/index.html).

Use compressed-json only if you cannot handle binary for some reason.


## Install

```bash
npm i compressed-json
```

## Usage

```js
'use strict'

const cjson = require('compressed-json')

// Convert json object
const compressed = cjson.compress({ /* Large json */ })
const restored = cjson.decompress(compressed)

// Convert json string
const compressedString = cjson.compress.toString({ /* Some large json */ })
const restoredFromString = cjson.decompress.fromString(compressedString)

```

## How It Works

compressed-json has two logics:

* [Key-Compression](#key-compression-logic)
* [String-Value-Pointing](#string-value-pointing-logic)

### Example compression

**example-src.json**

```json
{
  "description": "This is example json",
  "entities": [
    {
      "id": 100,
      "name": "Dog",
      "desc": "This is desc of dog",
      "tag": ["animal"]
    },
    {
      "id": 101,
      "name": "Cat",
      "desc": "This is desc of cat",
      "tag": ["animal"]
    }
  ],
  "notes": [
    "Unique string will be kept as is",
    "Duplicated string will be combined",
    "Duplicated string will be combined",
    "p: string start with 'p:' will be escaped "
  ]
}

```

**example-compressed.json**

```json
{
  "_": {
    "0": "This is example json",
    "1": [
      {
        "2": 100,
        "3": "Dog",
        "4": "This is desc of dog",
        "5": [
          "p:0"
        ]
      },
      {
        "2": 101,
        "3": "Cat",
        "4": "This is desc of cat",
        "5": [
          "p:0"
        ]
      }
    ],
    "6": [
      "Unique string will be kept as is",
      "p:1",
      "p:1",
      "e:p: string start with 'p:' will be escaped "
    ]
  },
  "K": [
    "description",
    "entities",
    "id",
    "name",
    "desc",
    "tag",
    "notes"
  ],
  "P": [
    "animal",
    "Duplicated string will be combined"
  ]
}
```

### Structure of compressed json

| Key | Description |
| --- | ----------- |
| `K` | Array of original keys. Object keys are replaced with index of `K` in the compressed json |
| `P` | Pointed string values. Values are replaced with index of `P` in the compressed json |
| `_` | Compressed payload. Keeps original structure, but keys and values may be replaced with pointers to `K` or `P` |


<a name="key-compression-logic"/>

### Key-Compression logic

All object keys are replaced with index of array stored in `K` of compressed JSON.

The more same key appears in original json, the more better compression.

<a name="string-value-pointing-logic" />

### String-Value-Pointing

String values appeared at least two will replaced with pointer string with contains index of array stored in `P` of compressed JSON.

The more same string value appears in original json, the more better compression.

---
_Source: https://npm.io/package/compressed-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
