# json-shrinker

> Minify JSON by removing whitespace and using scientific notation for numbers.

Latest version **2.0.0** (published 2026-02-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-02-14 |
| First published | 2019-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ecc521 |
| Keywords | json, minify, shrink, crush, small, compress |

## Links

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

## 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-02-14
- 1.0.4 — 2019-10-04
- 1.0.3 — 2019-10-03
- 1.0.2 — 2019-10-03

## README

# json-shrinker

A lightweight library to minify JSON by removing whitespace and using scientific notation for numbers. Now fully written in TypeScript with optional lossy compression features.

## Features

- **Whitespace Removal**: `{ "foo" : "bar" }` => `{"foo":"bar"}`
- **Scientific Notation**: `{"foo": 10000000000}` => `{"foo":1e10}`
- **TypeScript Support**: Fully typed.
- **Configurable**: Options for lossy compression (removing undefined/null, precision).
- **Zero Dependencies**: Lightweight.

## Installation

```bash
npm install json-shrinker
```

## Usage

### TypeScript / ES Modules

```typescript
import { shrinkJSON, stringify, ShrinkOptions } from 'json-shrinker';

// Shrink a JSON string
const bigJSONString = '{ "foo" : 10000 }';
const smallerJSON = shrinkJSON(bigJSONString);
console.log(smallerJSON); // {"foo":1e4}

// Stringify an object
const obj = { a: "hi", b: 10000000000, c: 0.00001 };
const smallJSON = stringify(obj);
console.log(smallJSON); // {"a":"hi","b":1e10,"c":1e-5}
```

### CommonJS

```javascript
const { shrinkJSON, stringify } = require("json-shrinker");

// ... same usage
```

## Configuration Options

You can pass an optional `options` object to `stringify`, `shrinkJSON`, etc.

```typescript
interface ShrinkOptions {
  removeUndefined?: boolean; // Default: false
  removeNull?: boolean;      // Default: false
  precision?: number;        // Default: undefined (no rounding)
}
```

### `removeUndefined` (Lossy)
If `true`, properties with `undefined` values in objects will be omitted (mimics standard `JSON.stringify` behavior).
Default is `false` (lossless), which converts them to `null` to preserve the key.

```javascript
stringify({ a: undefined }); // '{"a":null}'
stringify({ a: undefined }, { removeUndefined: true }); // '{}'
```

### `removeNull` (Lossy)
If `true`, properties with `null` values in objects will be omitted.

```javascript
stringify({ a: null }); // '{"a":null}'
stringify({ a: null }, { removeNull: true }); // '{}'
```

### `precision` (Lossy)
Limits the number of decimal places for numbers.

```javascript
stringify({ a: 1.23456789 }); // '{"a":1.23456789}'
stringify({ a: 1.23456789 }, { precision: 2 }); // '{"a":1.23}'
```

## API

### `stringify(value: any, options?: ShrinkOptions): string`
Stringifies a JavaScript value, applying shrinking techniques.

### `shrinkJSON(jsonString: string, options?: ShrinkOptions): string`
Parses a JSON string and re-stringifies it with shrinking.

## License

MIT

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