PHPackages                             inkrot/php-compress-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. inkrot/php-compress-json

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

inkrot/php-compress-json
========================

Store JSON data in space efficient manner

3.2.0(4mo ago)1070.8k↑32.2%3[2 issues](https://github.com/inkrot/php-compress-json/issues)1BSD-2-ClausePHPPHP ^8.0

Since Mar 20Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/inkrot/php-compress-json)[ Packagist](https://packagist.org/packages/inkrot/php-compress-json)[ Docs](http://github.com/inkrot/php-compress-json)[ RSS](/packages/inkrot-php-compress-json/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (7)Used By (1)

php-compress-json
=================

[](#php-compress-json)

A PHP library to compress large JSON documents with repeated structures and values. Helps to reduce the size of a JSON file, for example, during storage or transmission over the network, while preserving the document structure and key names.

Inspired by [compress-json](https://github.com/beenotung/compress-json). And thanks to [@beenotung](https://github.com/beenotung).

This library is fully compatible with [compress-json](https://github.com/beenotung/compress-json) for NodeJS.

Features
--------

[](#features)

- Supports all JSON types
- Object key order is preserved
- Repeated values are stored only once
- Numbers are encoded in [base62](https://en.wikipedia.org/wiki/Base62) format (0-9A-Za-z)
- Custom Memory providers for store and cache during compress heavy data (see class [AbstractMemory](./src/Memory/AbstractMemory.php)). You can pass your custom memory provider through the `Compressor::createWithMemory()` method
- Passing custom `json_encode` or `json_decode` arguments in `toJson` or `decompressJson` methods respectively

Special Values
--------------

[](#special-values)

The following special values are supported (v3.2.0 compatibility):

ValueEncoded`null``''` (empty string)`true``b|T``false``b|F``INF` (Infinity)`N|+``-INF` (-Infinity)`N|-``NAN` (Not a Number)`N|0`Example:

```
use CompressJson\Core\Compressor;

$data = [
    'infinity' => INF,
    'negInfinity' => -INF,
    'nan' => NAN,
];

$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();

// Decompress
$decompressed = Compressor::create()
    ->decompressJson($compressedJson);

var_dump($decompressed['infinity']);    // float(INF)
var_dump($decompressed['negInfinity']); // float(-INF)
var_dump(is_nan($decompressed['nan'])); // bool(true)
```

Install
-------

[](#install)

Via Composer

```
composer require inkrot/php-compress-json
```

Usage
-----

[](#usage)

### Compress

[](#compress)

```
use CompressJson\Core\Compressor;

$data = [
    'key1' => 'value1',
    'key2' => 'value3',
    'key3' => [
        'nestedKey1' => 'value1'
    ]
];
$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();

print_r($compressedJson); // encoded string in JSON format
```

Output:

```
[["key1","key2","key3","a|0|1|2","value1","value3","nestedKey1","a|6","o|7|4","o|3|4|5|8"],"9"]

```

---

### Decompress

[](#decompress)

```
use CompressJson\Core\Compressor;

$compressedJson = '[["key1","key2","key3","a|0|1|2","value1","value3","nestedKey1","a|6","o|7|4","o|3|4|5|8"],"9"]';
$data = Compressor::create()
    ->decompressJson($compressedJson);

print_r($data); // array
```

Output:

```
Array
(
    [key1] => value1
    [key2] => value3
    [key3] => Array
        (
            [nestedKey1] => value1
        )

)

```

---

### Example structure for efficient compression

[](#example-structure-for-efficient-compression)

```
use CompressJson\Core\Compressor;
$data = [
    'count' => 3,
    'names' => ['New York', 'London', 'Paris', 'Beijing', 'Moscow'],
    'cities' => [
        [
            'id' => 1,
            'name' => 'New York',
            'countryName' => 'USA',
            'location' => [
                'latitude' => 40.714606,
                'longitude' => -74.002800,
            ],
            'localityType' => 'BIG_CITY',
        ],
        [
            'id' => 2,
            'name' => 'London',
            'countryName' => 'UK',
            'location' => [
                'latitude' => 51.507351,
                'longitude' => -0.127696,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 3,
            'name' => 'Paris',
            'countryName' => 'France',
            'location' => [
                'latitude' => 48.856663,
                'longitude' => 2.351556,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 4,
            'name' => 'Beijing',
            'countryName' => 'China',
            'location' => [
                'latitude' => 39.901850,
                'longitude' => 116.391441,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
        [
            'id' => 5,
            'name' => 'Moscow',
            'countryName' => 'Russia',
            'location' => [
                'latitude' => 55.755864,
                'longitude' => 37.617698,
            ],
            'localityType' => 'COUNTRY_CAPITAL',
        ],
    ]
];
$compressedJson = Compressor::create()
    ->compress($data)
    ->toJson();

print_r(json_encode($data));
echo PHP_EOL;
print_r($compressedJson);
```

Pure JSON (749 chars)

```
{"count":3,"names":["New York","London","Paris","Beijing","Moscow"],"cities":[{"id":1,"name":"New York","countryName":"USA","location":{"latitude":40.714606,"longitude"
:-74.0028},"localityType":"BIG_CITY"},{"id":2,"name":"London","countryName":"UK","location":{"latitude":51.507351,"longitude":-0.127696},"localityType":"COUNTRY_CAPITAL
"},{"id":3,"name":"Paris","countryName":"France","location":{"latitude":48.856663,"longitude":2.351556},"localityType":"COUNTRY_CAPITAL"},{"id":4,"name":"Beijing","coun
tryName":"China","location":{"latitude":39.90185,"longitude":116.391441},"localityType":"COUNTRY_CAPITAL"},{"id":5,"name":"Moscow","countryName":"Russia","location":{"l
atitude":55.755864,"longitude":37.617698},"localityType":"COUNTRY_CAPITAL"}]}

```

Compressed JSON (562 chars)

```
[["count","names","cities","a|0|1|2","n|3","New York","London","Paris","Beijing","Moscow","a|5|6|7|8|9","id","name","countryName","location","localityType","a|B|C|D|E|F
","n|1","USA","latitude","longitude","a|J|K","n|e.2Xkv","n|-1C.28G","o|L|M|N","BIG_CITY","o|G|H|5|I|O|P","n|2","UK","n|p.dz7","n|-0.2vFR","o|L|T|U","COUNTRY_CAPITAL","o
|G|R|6|S|V|W","France","n|m.1XNq","n|2.2kQz","o|L|Z|a","o|G|4|7|Y|b|W","n|4","China","n|d.F7F","n|1s.bVh","o|L|f|g","o|G|d|8|e|h|W","n|5","Russia","n|t.1xtN","n|b.3lHA"
,"o|L|l|m","o|G|j|9|k|n|W","a|Q|X|c|i|o","o|3|4|A|p"],"q"]

```

In this example, compression gives an efficiency of 25%. However, the more complex and repetitive the structure, the greater the compression efficiency.

Testing
-------

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- [Islam Zaripov (author)](https://github.com/inkrot)
- [compress-json by Beeno Tung (the same library for NodeJS)](https://github.com/beenotung/compress-json)

License
-------

[](#license)

[BSD 2-Clause License](./LICENSE) (Free Open Sourced Software)

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance73

Regular maintenance activity

Popularity39

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~257 days

Total

5

Last Release

125d ago

Major Versions

v0.1.3 → 3.2.02026-01-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/7680ef60d34cf1f28df7342b1c9774032ddd74c9d55b8f1ef3d24947e99558bd?d=identicon)[inkrot](/maintainers/inkrot)

---

Top Contributors

[![inkrot](https://avatars.githubusercontent.com/u/37914508?v=4)](https://github.com/inkrot "inkrot (15 commits)")[![yuxaoxao](https://avatars.githubusercontent.com/u/19952652?v=4)](https://github.com/yuxaoxao "yuxaoxao (2 commits)")[![beenotung](https://avatars.githubusercontent.com/u/6007292?v=4)](https://github.com/beenotung "beenotung (1 commits)")

---

Tags

compress-jsonminify-jsonjsoncompress

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/inkrot-php-compress-json/health.svg)

```
[![Health](https://phpackages.com/badges/inkrot-php-compress-json/health.svg)](https://phpackages.com/packages/inkrot-php-compress-json)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k316.9M612](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15467.7M16](/packages/clue-ndjson-react)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
