json
json copied to clipboard
Bad JSON diff when removing object in array of object
What is the issue you have?
In this test A simple remove is expected as a diff. But the following exemple is not following the expected behavior: Old JSON:
{
"a": ["aa", "ab"]
}
New JSON:
{
"a": ["ab"]
}
The JSON Patch:
[
{
"op": "remove",
"path": "/a/1"
},
{
"op": "replace",
"path": "/a/0",
"value": "ab"
}
]
Please describe the steps to reproduce the issue.
- Create a JSON array containing multiple objects
- Remove an object but not the last one
What is the expected behavior?
The patch should be as following:
[
{
"op": "remove",
"path": "/a/0"
}
]
And what is the actual behavior instead?
The JSON Patch is currently as follow:
[
{
"op": "remove",
"path": "/a/1"
},
{
"op": "replace",
"path": "/a/0",
"value": "ab"
}
]
Which version of the library did you use?
Version: 3.9.1
I can confirm that the calculated patch is not the shortest one. However, it is correct, thus I removed the "bug" label. The currently implemented diff
algorithm is not optimal. PRs welcome.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I have the same trouble. for multi-level json example: They're just in a different order. base json:
{
"cost":[
{"count":0,"ccc":16.0},
{"count":2,"ccc":13.0}
]
}
target json:
{
"cost":[
{"count":2,"ccc":13.0},
{"count":0,"ccc":16.0}
]
}
patch result:
[
{
"op": "replace",
"path": "/status/cost/0/ccc",
"value": 16.0
},
{
"op": "replace",
"path": "/status/cost/0/count",
"value": 0
},
{
"op": "replace",
"path": "/status/cost/1/ccc",
"value": 13.0
},
{
"op": "replace",
"path": "/status/cost/1/count",
"value": 2
}
]
Is it possible to provide a good diff algorithm that considers these two jsons to be the same? Looking forward to your reply!^_^
Those two JSON files are not the same. Order of arrays is preserved in JSON. Order of elements in an object is not.
These two are equivalent:
{"count":0,"ccc":16.0},
{"ccc":16.0,"count":0},
These two are not:
[1,2,3,4,5,6,7,8,9,10]
[10,9,8,7,6,5,4,3,2,1]