Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Recursive JSON (RJSON) is data format and compressor.
Branch: master
Failed to load latest commit information.
bin Added nodejs package.js and bin/rjson for testing RJSON.{pack,unpack}…
test Added tests for complex data types and updated docs #2. Thanks to the @…
.gitignore Added nodejs package.js and bin/rjson for testing RJSON.{pack,unpack}…
LICENSE Added tests for complex data types and updated docs #2. Thanks to the @…
package.json Added unit test for use case when order of keys in JSON-format of pac…
rjson.js Added ISC LICENSE.

RJSON: compress JSON to JSON

RJSON (Recursive JSON) converts any JSON data collection into more compact recursive form. Compressed data is still JSON and can be parsed with JSON.parse. RJSON can compress not only homogeneous collections, but any data sets with free structure.

RJSON is single-pass stream compressor, it extracts data schemes from document, assign each schema unique number and use this number instead of repeating same property names again and again. Each data schema is unique set of keys ordered with natural sort (alphabetically). After packing and unpacking order of keys may be changed but restored document will be fully identical to the original document.

More information about RJSON data format you can find at my blog post Recursive JSON (RJSON) introduction published on IMO best social news RSS-reader.

There is also available RJSON demo where you can convert any JSON data into RJSON format, decode result and ensure that it matches original JSON data.

Bellow you can see same document in both forms.


"id": 7,
"tags": ["programming", "javascript"],
"users": [
    {"first": "Homer", "last": "Simpson"},
    {"first": "Hank", "last": "Hill"},
    {"first": "Peter", "last": "Griffin"}
"books": [
    {"title": "JavaScript", "author": "Flanagan", "year": 2006},
    {"title": "Cascading Style Sheets", "author": "Meyer", "year": 2004}


"id": 7,
"tags": ["programming", "javascript"],
"users": [
    {"first": "Homer", "last": "Simpson"},
    [2, "Hank", "Hill", "Peter", "Griffin"]
"books": [
    {"title": "JavaScript", "author": "Flanagan", "year": 2006},
    [3, "Cascading Style Sheets", "Meyer", 2004]

RJSON supports all types of data that JSON itself supports. When you will try to pack unsupported by JSON data types (for example Date) you may get unexpected results (see the discussion about this issue, look at test/tests.js for the examples). Please ensure before packing that your data contains only primitive values, arrays and plain objects.

RJSON allows to:

  • decrease JSON data redundancy before the compression with tradition tools like gzip (for example see great tool jsonpickle or Twitter API output when length of field name often greater than leghth of the value itself).

  • reduce JSON data size and network traffic when gzip isn't available. For example, in-browser 3D-modeling tools like Mydeco 3D-planner may process and send to server megabytes of JSON-data;

  • analyze large collections of JSON-data without unpacking of whole dataset. RJSON-data is still JSON-data, so it can be traversed and analyzed after parsing and fully unpacked only if a document meets some conditions.

The above JSON vs RJSON example is based on the data structure from the JSON DB: a compressed JSON format. It's concept is implemented in JSONH - JSON Homogeneous Collections Compressor. RJSON provides similar level of data compression like JSONH does, but RJSON isn't limited to homogeneous collections only.

The library file rjson.js have no external dependencies but if you use NodeJS you can install it with npm install rjson. To run unit tests just open test/index.html in your browser or execute npm test in the console.

For testing RJSON compression you can use bin/rjson script. It reads JSON/RJSON input from stdin and outputs RJSON/JSON to stdout. To unpack RJSON data try rjson -u. If you want to see some stat about comprerssion ratio and time, use rjson -v. With rjson -t you can active test mode in which script will compare restored and original data. For example:

$ cat ./test/fixtures/twitter_search100.json | rjson -v > ./100.rjson
In: 100523, Out: 64664, In/Out=155%, Time: 22ms (RJSON: 10ms).

$ cat ./100.rjson | rjson -uv > ./100.json
In: 64664, Out: 100523, In/Out=64%, Time: 21ms (RJSON: 4ms).

$ curl "" | rjson -vt > /dev/null
In: 103943, Out: 65763, In/Out=158%, Time: 32ms (RJSON: 18ms).

Dmitri Russu kindly ported RJSON to PHP, you can obtain it at RJSON-php.

Fell free to compress the world.

Follow me on Twitter.

Something went wrong with that request. Please try again.