Controlling how inner objects are handled when computing diff
Hi! I am using Json.diff with play json and I am having an undesired result when dealing with nested objects in my array. When I have two json that look like this:
json1
{ "fields":{ "Foo": {"id": 3} } }
and
json2
{ "fields":{ "Foo": {"id": 5} } }
when doing
JsonDiff.diff(json1, json2, remember = false)
I am getting this diff:
[ { "op" : "replace", "path" : "/fields/Foo/id", "value" : "4" }]
When the desired result is this:
[ { "op" : "replace", "path" : "/fields/Foo", "value" : {"id": "4"} }]
Is there any way I can control how the diff is generated to it can match the second result?
Thanks, Juan
Hello @juangrases, I'd say that the current behavior is correct as applying it to the json1 value will produce json2. That's all what patches are about.
The diff algorithm generates the smallest (arguably) diff possible, meaning that it produces operations on paths that are as specific as possible. This is achieved by going as deep as possible into nested object, which the case here.
You can always implement your own diff algorithm if you want some specific shape of diffs, but in this case, what criterion would you use to decided that the diff shouldn't go one step further in the nested object? Can I also ask why you want this specific shape of diff and not the produced one?
Hi @satabin, thanks a lot for your answer, the reason I want this specific shape is because it looks like the REST API of Azure devops, which I am integrating with, expect that format when updating a workItem.
[ { "op" : "replace", "path" : "/fields/System.AssignedTo", "value" : {"id": "15a1aa62-a74a-6fd2-a844-42cea9e79fc6"} }]
Works fine but when I try to do:
[ { "op" : "replace", "path" : "/fields/System.AssignedTo/id", "value" : "15a1aa62-a74a-6fd2-a844-42cea9e79fc6" }]
I get a 400 response with a message:
"Unable to evaluate path /fields/System.AssignedTo/id.",
I will look into implementing my own diff algorithm and maybe explore more the API to see if there is another way to set the value.
This is interesting. Is it only happening with fields named id?