json-diff icon indicating copy to clipboard operation
json-diff copied to clipboard

Unexpected diff when comparing similar objects

Open andreasfreier opened this issue 2 years ago • 1 comments

For the following two similar objects

a = [
  {key: 'a', value: '0'},
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]


b = [
  {key: 'a', value: '0'},
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]

JsonDiff.diff(a, b) returns the following diff

[{"op"=>"move", "from"=>"/7", "path"=>"/1"}, {"op"=>"move", "from"=>"/2", "path"=>"/7"}]

which is replacing the similar lines with each other.

Strangely, if we remove any of the a/c/d/e/f/g lines in both objects, the bug isn't there anymore, e.g.

a = [
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]


b = [
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]

JsonDiff.diff(a, b)
=> []

andreasfreier avatar Nov 29 '23 09:11 andreasfreier

PS. Found with json-diff 0.4.1

andreasfreier avatar Nov 29 '23 09:11 andreasfreier