# jsonpack

> A compression algorithm for JSON

Latest version **2.0.0** (published 2026-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonpack
pnpm add jsonpack
yarn add jsonpack
bun add jsonpack
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-06-20 |
| First published | 2013-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 0 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 322 |
| Author | Rodrigo González |
| Maintainers | sapienlab, roro |
| Keywords | compress, json, pack, unpack |

## Links

- npm: https://www.npmjs.com/package/jsonpack
- Repository: https://github.com/rgcl/jsonpack
- Issues: https://github.com/rgcl/jsonpack/issues
- npm.io page: https://npm.io/package/jsonpack

## 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

- 2.0.0 (latest) — 2026-06-20
- 1.1.5 — 2016-04-20
- 1.1.4 — 2015-01-09
- 1.1.2 — 2014-09-20
- 1.1.1 — 2014-08-09
- 1.1.0 — 2014-07-08
- 1.0.1 — 2014-03-09
- 1.0.0 — 2013-03-22

## README

# jsonpack

[![Test](https://github.com/rgcl/jsonpack/actions/workflows/test.yml/badge.svg)](https://github.com/rgcl/jsonpack/actions/workflows/test.yml)
[![npm](https://img.shields.io/npm/v/jsonpack)](https://www.npmjs.com/package/jsonpack)

A URL-safe JSON serializer. Produces compact ASCII output usable directly in URLs and `localStorage` — no base64, no binary, no extra encoding step.

## Installation

```bash
npm install jsonpack
```

## Usage

**CommonJS**
```js
const { pack, unpack } = require('jsonpack');
```

**ESM**
```js
import { pack, unpack } from 'jsonpack';
```

**Example**
```js
const { pack, unpack } = require('jsonpack');

const data = {
    type: 'FeatureCollection',
    features: [
        { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.98, 40.74] }, properties: { name: 'A' } },
        { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.99, 40.75] }, properties: { name: 'B' } },
        // ... hundreds more
    ]
};

const packed = pack(data);
// repeated keys like "type", "Feature", "geometry", "Point", "coordinates"
// are stored once in a dictionary and referenced by index

const restored = unpack(packed);
```

## API

### `pack(json, options?)`

Serializes a JSON value into a compact URL-safe string.

- `json` — any JSON-serializable value, or a JSON string
- `options.verbose` — log each step to console (default: `false`)
- `options.debug` — return internal representation instead of string (default: `false`)

`Date` objects are preserved: `unpack(pack(date))` returns a `Date` instance, not a string.

Returns a `string`.

### `unpack(packed, options?)`

Restores the original value from a packed string.

- `packed` — string produced by `pack()`
- `options.verbose` — log each step to console (default: `false`)

Returns the original value.

---

## Why jsonpack

The standard way to embed JSON in a URL or `localStorage` is:

```js
encodeURIComponent(JSON.stringify(data))
```

It works, but it *expands* your data — `{`, `"`, `:` become `%7B`, `%22`, `%3A`. A typical API response grows to **140–170% of its original size**.

The alternative with the best compression, [lz-string](https://github.com/pieroxy/lz-string), shrinks data dramatically but decodes **4–6× slower**.

jsonpack sits in between: **< 7 KB minified, zero dependencies** (no transitive dependencies either).

### Benchmark

Measured across 8 real-world datasets (GeoJSON, e-commerce, API responses, deeply nested structures):

![URL-safe JSON serializers: compression vs speed](charts/01-scatter-compression-vs-speed.png)

| | encodeURI(JSON) | **jsonpack** | lz-string (URI) |
|---|:---:|:---:|:---:|
| Avg output size | 152% of original | **68% of original** | 39% of original |
| Avg unpack speed | 262 MB/s | **171 MB/s** | 41 MB/s |
| Zero dependencies | ✓ | ✓ | ✓ |
| URL-safe output | ✓ | ✓ | ✓ |

#### Compression by dataset

![Compression ratio by dataset](charts/02-ratio-per-dataset.png)

#### Unpack speed by dataset

![Unpack speed by dataset](charts/03-unpack-speed-per-dataset.png)

Full benchmark methodology and raw results: [rgcl/jsonpack-benchmark](https://github.com/rgcl/jsonpack-benchmark)

### When to use jsonpack

**Use jsonpack when:**
- You need to store JSON in a URL query string or `localStorage`
- Output size matters (jsonpack produces ~55% less data than `encodeURIComponent`)
- Fast decoding matters (jsonpack decodes 4× faster than lz-string)
- You can't afford to grow your bundle

**Use lz-string instead when** output size is the only constraint and decoding speed doesn't matter.

**Use `encodeURIComponent` when** the data is small, changes rarely, or you want zero abstraction.

### How it works

jsonpack builds a dictionary of all unique values (strings, integers, floats, dates) in the JSON and replaces them with base-36 indices. The result is a flat, ASCII-only string. Repeated keys and values — common in structured data like API responses and GeoJSON — are stored once and referenced everywhere.

`Date` objects are stored as ISO 8601 strings in the dictionary and marked with a special token in the structure, so `unpack` can restore them as `Date` instances. This is one area where jsonpack goes beyond what `JSON.parse(JSON.stringify())` offers natively.

---

## Notes

- Pack and unpack are synchronous. For large payloads in a browser, run them in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
- Requires Node.js ≥ 14.
- The packed format is not binary-compatible with other JSON compression libraries.

## Licence

MIT © 2013 Rodrigo González, SASUD

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