Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff shows wrong elements as changed in a list of objects #18

Closed
nhinds opened this issue Nov 4, 2015 · 1 comment
Closed

Diff shows wrong elements as changed in a list of objects #18

nhinds opened this issue Nov 4, 2015 · 1 comment

Comments

@nhinds
Copy link

nhinds commented Nov 4, 2015

For some inputs, the diff shows an existing item in the list as added, and the new item as only having one field modified:

$ cat one.json
[
  { "key": "value1", "foo": "exists", "bar": "exists" }
]

$ cat two.json
[
  { "key": "value2", "foo": "new", "bar": "new" },
  { "key": "value2", "foo": "exists", "bar": "exists" }
]

$ json-diff one.json two.json
 [
   {
-    key: "value1"
+    key: "value2"
   }
+  {
+    key: "value2"
+    foo: "exists"
+    bar: "exists"
+  }
 ]

The diff above shows the { "key": "value2", "foo": "exists", "bar": "exists" } as being added, and fails to output the "new" string values at all.

If the list items are reversed in two.json, the diff output is even more wrong:

$ cat one.json
[
  { "key": "value1", "foo": "exists", "bar": "exists" }
]
$ cat two.json
[
  { "key": "value2", "foo": "exists", "bar": "exists" },
  { "key": "value2", "foo": "new", "bar": "new" }
]
$ json-diff one.json two.json
 [
   {
-    key: "value1"
+    key: "value2"
-    foo: "exists"
+    foo: "new"
-    bar: "exists"
+    bar: "new"
   }
+  {
+    key: "value2"
+    foo: "new"
+    bar: "new"
+  }
 ]

In this case, it shows the existing item as being removed, and the new item as being added twice.

dbushong added a commit to groupon/assertive that referenced this issue Jul 14, 2017
I'd like a prettier diff like the one from json-diff,
but its array handling is fatally broken at the moment

See: andreyvit/json-diff#18
@ewoudenberg
Copy link
Collaborator

This has been fixed in 0.6.0:

$ cat one.json two.json
[
  { "key": "value1", "foo": "exists", "bar": "exists" }
]
[
  { "key": "value2", "foo": "new", "bar": "new" },
  { "key": "value2", "foo": "exists", "bar": "exists" }
]
$ json-diff  one.json two.json
 [
+  {
+    key: "value2"
+    foo: "new"
+    bar: "new"
+  }
   {
-    key: "value1"
+    key: "value2"
   }
 ]
$ json-diff  two.json one.json
 [
-  {
-    key: "value2"
-    foo: "new"
-    bar: "new"
-  }
   {
-    key: "value2"
+    key: "value1"
   }
 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants