zgrossbart / jdd

A semantic JSON compare tool
http://www.jsondiff.com
Apache License 2.0
1.03k stars 180 forks source link

compare entire object rather order of object in array #11

Closed rajniprabha closed 7 years ago

rajniprabha commented 7 years ago

situation is - one json object has four four object in a array and another object has 3 object in a array (same as first but the second one removed) - ideally it should show only second object removed in the output, but its showing second and third object is updated and fourth one is deleted. JSON 1: { "modules": [ { "id": 454, "code": "DPI-NDPI-URL", "description": "nDPI which supports HTTP and HTTPS only", "expiry-date": "2017-06-21", "units": 0 }, { "id": 456, "code": "DPI-NDPI-FULL", "description": "nDPI DPI library currently maintained by ntop", "expiry-date": "2017-06-22", "units": 0 }, { "id": 457, "code": "DPI-NDPI-URL-SIG", "description": "ndpi\"s HTTP/HTTPS only signature updates signature, requires DPI-NDPI-URL", "expiry-date": "2017-06-14", "units": 0 }, { "id": 458, "code": "FLOWnew", "description": "Generic module for enabling flows", "expiry-date": "2017-06-21", "units": 0 } ]}

JSON 2:

{ "modules": [ { "id": 454, "code": "DPI-NDPI-URL", "description": "nDPI which supports HTTP and HTTPS only", "expiry-date": "2017-06-21", "units": 0 }, { "id": 457, "code": "DPI-NDPI-URL-SIG", "description": "ndpi\"s HTTP/HTTPS only signature updates signature, requires DPI-NDPI-URL", "expiry-date": "2017-06-14", "units": 0 }, { "id": 458, "code": "FLOWnew", "description": "Generic module for enabling flows", "expiry-date": "2017-06-21", "units": 0 } ] }

output its showing { "type": "update", "key": "modules", "embededKey": "$index", "changes": [ { "type": "update", "key": "1", "changes": [ { "type": "update", "key": "id", "value": 457, "oldValue": 456 }, { "type": "update", "key": "code", "value": "DPI-NDPI-URL-SIG", "oldValue": "DPI-NDPI-FULL" }, { "type": "update", "key": "description", "value": "ndpi\"s HTTP/HTTPS only signature updates signature, requires DPI-NDPI-URL", "oldValue": "nDPI DPI library currently maintained by ntop" }, { "type": "update", "key": "expiry-date", "value": "2017-06-14", "oldValue": "2017-06-22" } ] }, { "type": "update", "key": "2", "changes": [ { "type": "update", "key": "id", "value": 458, "oldValue": 457 }, { "type": "update", "key": "code", "value": "FLOWnew", "oldValue": "DPI-NDPI-URL-SIG" }, { "type": "update", "key": "description", "value": "Generic module for enabling flows", "oldValue": "ndpi\"s HTTP/HTTPS only signature updates signature, requires DPI-NDPI-URL" }, { "type": "update", "key": "expiry-date", "value": "2017-06-21", "oldValue": "2017-06-14" } ] }, { "type": "remove", "key": "3", "value": { "id": 458, "code": "FLOWnew", "description": "Generic module for enabling flows", "expiry-date": "2017-06-21", "units": 0 } } ] }

zgrossbart commented 7 years ago

Thank you for using JSONDiff and a big thank you for the bug report. This is very similar to issue #5. The problem is that in JSON data is is very difficult to tell that object three on the left side is object three on the right side when they are in a different position in the array.

JSON objects don't have IDs and you can't use a compare to match them. If the left-side object is "my string" and the right side object is "my string2" does that mean they are the same object with a change or a different object. It gets even more complicated when it looks like this:

Left                         Right
[                            [
    "a",                         "a",
    "b",                         "a",
    "a",                         "a",
    "a",                         "a",
    "a"                      ]
]                        

What does this case mean? Does this mean that the second object was changed from "b" to "a" or that the second object was delete from the array?

There's no good way for JSONDiff to know which is which. Your feature would be awesome, but it's fundamentally impossible to implement.

At least, I don't see a good way right now. Do you have any good ideas?