PHPackages                             steamulo/json-diff - 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. steamulo/json-diff

AbandonedLibrary

steamulo/json-diff
==================

JSON diff/rearrange/patch/pointer library for PHP

v3.10.4.2(2y ago)08.0k↓100%MITPHP

Since Nov 9Pushed 2y agoCompare

[ Source](https://github.com/STEAMULO/json-diff)[ Packagist](https://packagist.org/packages/steamulo/json-diff)[ RSS](/packages/steamulo-json-diff/feed)WikiDiscussions fixOptions Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (0)

JSON diff/rearrange/patch/pointer library for PHP
=================================================

[](#json-diffrearrangepatchpointer-library-for-php)

A PHP implementation for finding unordered diff between two `JSON` documents.

[![Build Status](https://camo.githubusercontent.com/6677ecfac4f8e625eb8da5eb1f2398cde90c12ac810f2eface5365b89ba7735a/68747470733a2f2f7472617669732d63692e6f72672f73776167676573742f6a736f6e2d646966662e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/swaggest/json-diff)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/553cae83cb702cbcb1135645f8367c820ec40fab54739425ceb8d91992512685/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73776167676573742f6a736f6e2d646966662f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/swaggest/json-diff/?branch=master)[![Code Climate](https://camo.githubusercontent.com/efbd39977707423cfab588d19c728e2cb48d49d9f5f74c263a9e395dd60e2a56/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f73776167676573742f6a736f6e2d646966662f6261646765732f6770612e737667)](https://codeclimate.com/github/swaggest/json-diff)[![Code Coverage](https://camo.githubusercontent.com/e1ca1edfaa455421326740b178feabd0e21c6626ff798653c5ec8197a7a5a505/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73776167676573742f6a736f6e2d646966662f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/swaggest/json-diff/code-structure/master/code-coverage)[![time tracker](https://camo.githubusercontent.com/f8cd0b8b539afea1cea9604a56e706bb605ed5575a540e4fd4af15a445ff3fd7/68747470733a2f2f77616b6174696d652e636f6d2f62616467652f6769746875622f73776167676573742f6a736f6e2d646966662e737667)](https://wakatime.com/badge/github/swaggest/json-diff)

Purpose
-------

[](#purpose)

- To simplify changes review between two `JSON` files you can use a standard `diff` tool on rearranged pretty-printed `JSON`.
- To detect breaking changes by analyzing removals and changes from original `JSON`.
- To keep original order of object sets (for example `swagger.json` [parameters](https://swagger.io/docs/specification/describing-parameters/) list).
- To [make](#getpatch) and [apply](#jsonpatch) JSON Patches, specified in [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902) from the IETF.
- To [make](#getmergepatch) and [apply](#jsonmergepatch) JSON Merge Patches, specified in [RFC 7386](https://datatracker.ietf.org/doc/html/rfc7386) from the IETF.
- To retrieve and modify data by [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901).
- To recursively replace by JSON value.

Installation
------------

[](#installation)

### Library

[](#library)

```
git clone https://github.com/swaggest/json-diff.git
```

### Composer

[](#composer)

[Install PHP Composer](https://getcomposer.org/doc/00-intro.md)

```
composer require swaggest/json-diff
```

Library usage
-------------

[](#library-usage)

### `JsonDiff`

[](#jsondiff)

Create `JsonDiff` object from two values (`original` and `new`).

```
$r = new JsonDiff(json_decode($originalJson), json_decode($newJson));
```

On construction `JsonDiff` will build `rearranged` value of `new` recursively keeping `original` keys order where possible. Keys that are missing in `original` will be appended to the end of `rearranged` value in same order they had in `new` value.

If two values are arrays of objects, `JsonDiff` will try to find a common unique field in those objects and use it as criteria for rearranging. You can enable this behaviour with `JsonDiff::REARRANGE_ARRAYS` option:

```
$r = new JsonDiff(
    json_decode($originalJson),
    json_decode($newJson),
    JsonDiff::REARRANGE_ARRAYS
);
```

Available options:

- `REARRANGE_ARRAYS` is an option to enable [arrays rearrangement](#arrays-rearrangement) to minimize the difference.
- `STOP_ON_DIFF` is an option to improve performance by stopping comparison when a difference is found.
- `JSON_URI_FRAGMENT_ID` is an option to use URI Fragment Identifier Representation (example: "#/c%25d"). If not set default JSON String Representation (example: "/c%d").
- `SKIP_JSON_PATCH` is an option to improve performance by not building JsonPatch for this diff.
- `SKIP_JSON_MERGE_PATCH` is an option to improve performance by not building JSON Merge Patch value for this diff.
- `TOLERATE_ASSOCIATIVE_ARRAYS` is an option to allow associative arrays to mimic JSON objects (not recommended).
- `COLLECT_MODIFIED_DIFF` is an option to enable [getModifiedDiff](#getmodifieddiff).

Options can be combined, e.g. `JsonDiff::REARRANGE_ARRAYS + JsonDiff::STOP_ON_DIFF`.

#### `getDiffCnt`

[](#getdiffcnt)

Returns total number of differences

#### `getPatch`

[](#getpatch)

Returns [`JsonPatch`](#jsonpatch) of difference

#### `getMergePatch`

[](#getmergepatch)

Returns [JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7386) value of difference

#### `getRearranged`

[](#getrearranged)

Returns new value, rearranged with original order.

#### `getRemoved`

[](#getremoved)

Returns removals as partial value of original.

#### `getRemovedPaths`

[](#getremovedpaths)

Returns list of `JSON` paths that were removed from original.

#### `getRemovedCnt`

[](#getremovedcnt)

Returns number of removals.

#### `getAdded`

[](#getadded)

Returns additions as partial value of new.

#### `getAddedPaths`

[](#getaddedpaths)

Returns list of `JSON` paths that were added to new.

#### `getAddedCnt`

[](#getaddedcnt)

Returns number of additions.

#### `getModifiedOriginal`

[](#getmodifiedoriginal)

Returns modifications as partial value of original.

#### `getModifiedNew`

[](#getmodifiednew)

Returns modifications as partial value of new.

#### `getModifiedDiff`

[](#getmodifieddiff)

Returns list of [`ModifiedPathDiff`](src/ModifiedPathDiff.php) containing paths with original and new values.

Not collected by default, requires `JsonDiff::COLLECT_MODIFIED_DIFF` option.

#### `getModifiedPaths`

[](#getmodifiedpaths)

Returns list of `JSON` paths that were modified from original to new.

#### `getModifiedCnt`

[](#getmodifiedcnt)

Returns number of modifications.

### `JsonPatch`

[](#jsonpatch)

#### `import`

[](#import)

Creates `JsonPatch` instance from `JSON`-decoded data.

#### `export`

[](#export)

Creates patch data from `JsonPatch` object.

#### `op`

[](#op)

Adds operation to `JsonPatch`.

#### `apply`

[](#apply)

Applies patch to `JSON`-decoded data.

#### `setFlags`

[](#setflags)

Alters default behavior.

Available flags:

- `JsonPatch::STRICT_MODE` Disallow converting empty array to object for key creation.
- `JsonPatch::TOLERATE_ASSOCIATIVE_ARRAYS` Allow associative arrays to mimic JSON objects (not recommended).

### `JsonPointer`

[](#jsonpointer)

#### `escapeSegment`

[](#escapesegment)

Escapes path segment.

#### `splitPath`

[](#splitpath)

Creates array of unescaped segments from `JSON Pointer` string.

#### `buildPath`

[](#buildpath)

Creates `JSON Pointer` string from array of unescaped segments.

#### `add`

[](#add)

Adds value to data at path specified by segments.

#### `get`

[](#get)

Gets value from data at path specified by segments.

#### `getByPointer`

[](#getbypointer)

Gets value from data at path specified `JSON Pointer` string.

#### `remove`

[](#remove)

Removes value from data at path specified by segments.

### `JsonMergePatch`

[](#jsonmergepatch)

#### `apply`

[](#apply-1)

Applies patch to `JSON`-decoded data.

### `JsonValueReplace`

[](#jsonvaluereplace)

#### `process`

[](#process)

Recursively replaces all nodes equal to `search` value with `replace` value.

Example
-------

[](#example)

```
$originalJson =
