1.0.1 2018-03-09 17:02 UTC

This package is auto-updated.

Last update: 2024-05-17 01:43:07 UTC


Get a readable diff from two bits of JSON.


composer require konsulting/json-diff


There are a few ways to use the class. It depends on your preference, see the example below. It is also possible to exclude a set of keys from the diff, also shown below.


use Konsulting\JsonDiff;

// Using 'new'
$diff = (new JsonDiff('["json"]'))->exclude(['key'])->compareTo('["different_json"]');

// Using a simple factory method
$diff = JsonDiff::original('["json"]')->exclude(['key'])->compareTo('["different_json"]');

// Using a simple 'all-in-one' static method.
$diff = JsonDiff::compare($original = '["json"]', $new = '["different_json"]', $exclude = ['key']);

The output is a JsonDiffResult that stores the following information:

  • original - the original json as an array
  • new - the new json as an array
  • added - what was added
  • removed - what was removed
  • diff - a combined diff of added and removed as an array
  • changed - a boolean signifying if there was a relevant change

The JsonDiffResult allows access of the result in several ways:

        'original' => ['...'],
        'new' => ['...'],
        'diff' => [
            'added' => ['...'],
            'removed' => ['...']
        'changed' => true // or false if nothing changed
    $diff->toJson(); // Json representation of toArray()
    // All the properties can be accessed using array or object notation;
    // original, new, added, removed, diff, changed.


Contributions are welcome and will be fully credited. We will accept contributions by Pull Request.


  • Use the PSR-2 Coding Standard
  • Add tests, if you’re not sure how, please ask.
  • Document changes in behaviour, including readme.md.


We use PHPUnit

Run tests using PHPUnit: vendor/bin/phpunit