Home

Awesome

json-diff

Take two Ruby objects that can be serialized to JSON. Output an array of operations (additions, deletions, moves) that would convert the first one to the second one.

gem install json-diff  # Or `gem 'json-diff'` in your Gemfile.
require 'json-diff'
JsonDiff.diff(1, 2)
#> [{'op' => 'replace', 'path' => '', 'value' => 2}]

Outputs RFC6902. Look at hana for a JSON patch algorithm that can use this output.

Options

* Changing this option prevents the use of the output for JSON patching.

You can also install its associated program to use this on the command line:

gem install json-diff  # On Linux, you may need to use sudo
json-diff before.json after.json

Heart

Pros:

Cons:

Comparisons

HashDiff — LCS, no move operation.

require "json-diff"
JsonDiff.diff([1, 2, 3, 4, 5], [6, 4, 3, 2])
# [{'op' => 'remove', 'path' => '/4'},
#  {'op' => 'remove', 'path' => '/0'},
#  {'op' => 'move', 'from' => '/0', 'path' => '/2'},
#  {'op' => 'move', 'from' => '/1', 'path' => '/0'},
#  {'op' => 'add', 'path' => '/0', 'value' => 6}]

require "hashdiff"
HashDiff.diff([1, 2, 3, 4, 5], [6, 4, 3, 2])
# [["-", "[0]", 1],
#  ["+", "[0]", 6],
#  ["+", "[1]", 4],
#  ["+", "[2]", 3],
#  ["-", "[6]", 5],
#  ["-", "[5]", 4],
#  ["-", "[4]", 3]]

jsondiff — no similitude, no LCS.

require "json-diff"
JsonDiff.diff(
  [{'code' => "ABW", 'name' => "Abbey Wood"}, {'code' => "KGX", 'name' => "Kings Cross"}],
  [{'code' => "KGX", 'name' => "Kings Cross"}, {'code' => "ABW", 'name' => "Abbey Wood"}]
)
# [{'op' => 'move', 'from' => '/0', 'path' => '/1'}]

require "jsondiff"
JsonDiff.generate(
  [{'code' => "ABW", 'name' => "Abbey Wood"}, {'code' => "KGX", 'name' => "Kings Cross"}],
  [{'code' => "KGX", 'name' => "Kings Cross"}, {'code' => "ABW", 'name' => "Abbey Wood"}]
)
# [{:op => :replace, :path => '/0/code', :value => 'KGX'},
#  {:op => :replace, :path => '/0/name', :value => 'Kings Cross'},
#  {:op => :replace, :path => '/1/code', :value => 'ABW'},
#  {:op => :replace, :path => '/1/name', :value => 'Abbey Wood'}]

Plans & Bugs

Roughly ordered by priority.


See the LICENSE file for licensing information.