# json-difference

> json difference lib

Latest version **1.16.3** (published 2026-04-24) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 55/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.16.3 |
| Published | 2026-04-24 |
| First published | 2018-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.17.x |
| Dependencies | 0 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 58 |
| Author | lukascivil |
| Maintainers | lukascivil |
| Keywords | json, diff |

## Links

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

## 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.16.3 (latest) — 2026-04-24
- 1.16.2 — 2026-04-24
- 1.16.1 — 2024-02-04
- 1.16.0 — 2023-12-27
- 1.15.8 — 2023-12-27
- 1.15.7 — 2023-08-06
- 1.9.1 — 2023-01-22
- 1.9.0 — 2023-01-22
- 1.8.2 — 2022-09-05
- 1.8.0 — 2022-08-21
- 1.6.1 — 2022-08-17
- 1.6.0 — 2022-08-17
- 1.5.0 — 2022-08-16
- 1.4.0 — 2022-08-16
- 1.3.0 — 2022-08-16
- … 8 more at https://npm.io/package/json-difference/versions

## README

# json-difference

[![npm version](http://img.shields.io/npm/v/json-difference.svg?style=flat)](https://www.npmjs.com/package/json-difference)
[![Total Downloads](https://img.shields.io/npm/dt/json-difference.svg)](https://www.npmjs.com/package/json-difference)
[![MIT License](https://img.shields.io/npm/l/json-difference.svg?style=flat)](https://github.com/lukascivil/jsondiffer/blob/master/LICENSE)

Computes the difference between two JSON structures and returns an intuitive path-based delta `{ added, removed, edited }`. Fast even on large payloads.

🪶 Lightweight: **1.95 kB** (gzip: **0.79 kB**). Zero runtime dependencies.

## Installation

```sh
yarn add json-difference
# or
npm install json-difference
```

**Requirements:** Node.js `>=18.17`.

## Usage

```ts
import { getDiff } from 'json-difference'

const oldStruct = { color: { color1: 'black', color2: 'brown' }, special: true }
const newStruct = { color: { color1: 'red', color2: 'blue' }, special2: false }

getDiff(oldStruct, newStruct)
// {
//   added:   [["special2", false]],
//   removed: [["special", true]],
//   edited:  [
//     ["color/color1", "black", "red"],
//     ["color/color2", "brown", "blue"]
//   ]
// }
```

### Lodash-style paths

```ts
getDiff(oldStruct, newStruct, { isLodashLike: true })
// edited: [["color.color1", "black", "red"], ...]
```

## API

```ts
import {
  getDiff,
  getStructPaths,
  getEditedPaths,
  getPathsDiff,
  // types
  Delta,
  EditedPath,
  PathsDiff,
  StructPaths,
  JsonDiffOptions
} from 'json-difference'
```

### `getDiff(old, new, options?) => Delta`

High-level entry point. Returns a full delta.

```ts
interface Delta {
  added:   Array<[path: string, value: unknown]>
  removed: Array<[path: string, value: unknown]>
  edited:  Array<[path: string, oldValue: unknown, newValue: unknown]>
}
```

Accepts objects, arrays, or JSON-encoded strings for both inputs.

### `getStructPaths(json, isLodashLike?) => StructPaths`

Flattens a JSON into a plain object mapping **dot/slash paths → leaf values**. Building block used internally by `getDiff`.

```ts
getStructPaths({ a: { b: 1 }, c: [true] })
// { "a/b": 1, "c/0[]": true }
```

### `getEditedPaths(oldPaths, newPaths) => EditedPath[]`

Given two flattened path maps, returns only the paths whose value changed.

### `getPathsDiff(pathsA, pathsB) => PathsDiff[]`

Returns the paths present in `pathsA` but missing from `pathsB` (set difference). `getDiff` uses this with `(old, new)` for `removed` and with `(new, old)` for `added`.

## Options

| Option | Type | Default | Description |
|---|---|---|---|
| `isLodashLike` | `boolean` | `false` | Use lodash-style bracket notation (`a.b[0]`) instead of slash notation (`a/b/0[]`) |

## Path format

| Marker | Meaning |
|---|---|
| `__root__` | Root object/array was replaced entirely |
| `@{}` | Non-leaf node of type object |
| `@[]` | Non-leaf node of type array |

### Reference operations

| Original | Modified | Delta |
|---|---|---|
| `{}` | `[]` | `edited: [["__root__", {}, []]]` |
| `{"a":"b"}` | `{"a":"c"}` | `edited: [["a", "b", "c"]]` |
| `{}` | `{"a":"b"}` | `added: [["a", "b"]]` |
| `{"a":"b"}` | `{}` | `removed: [["a", "b"]]` |
| `[{}]` | `[]` | `removed: [["0[]", {}]]` |

## Browser usage (CDN)

```html
<script type="module">
  import { getDiff } from 'https://json-difference.s3.amazonaws.com/1.16.1/json-difference-1.16.1.mjs'
  console.log(getDiff({ a: 1 }, { a: 2 }))
</script>
```

## Related packages

- [`json-difference-cli`](https://www.npmjs.com/package/json-difference-cli) — CLI wrapper (`jd -o … -m …`)
- [`mcp-json-diff`](https://github.com/lukascivil/json-difference/tree/master/tools/mcp-json-diff) — MCP server for AI agents (Claude, Cursor, etc.)

## Links

- Repository: <https://github.com/lukascivil/json-difference>
- Live playground: <https://lukascivil.github.io/json-difference/>
- Issues: <https://github.com/lukascivil/json-difference/issues>
- [Changelog](./CHANGELOG.md)

## License

[MIT](../../LICENSE) © lukascivil

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