2

I am new to Json stuff i.e. JSON PATCH. I have scenario where I need to figure out between two version of Json files of same object, for that I am using json-patch-master. But unfortunately the patch generated interpreting it differently i.e. the order differently hence getting unexpected/invalid results. Could anyone help me how to preserve the order while generating Json Patch ?

**Here is the actual example.
Original Json file :**

[ {
  "name" : "name1",
  "roolNo" : "1"
}, {
  "name" : "name2",
  "roolNo" : "2"
}, {
  "name" : "name3",
  "roolNo" : "3"
}, {
  "name" : "name4",
  "roolNo" : "4"
} ]


**Modified/New Json file:  i.e. removed 2nd node of original file.**

[ {
  "name" : "name1",
  "roolNo" : "1"
}, {
  "name" : "name3",
  "roolNo" : "3"
}, {
  "name" : "name4",
  "roolNo" : "4"
} ]

**Patch/Diff Generated :**

[ {"op":"remove","path":"/3"},
{"op":"replace","path":"/1/name","value":"name3"},
{"op":"replace","path":"/1/roolNo","value":"3"},
{"op":"replace","path":"/2/name","value":"name4"},
{"op":"replace","path":"/2/roolNo","value":"4"}]

Very time I generate Diff/Patch it is giving different path/diff results.
And moreover the interpretation is different i.e. order is not preserving.

**Is there any way to get expected results i.e. [ {"op":"remove","path":"/1"} ] , in other words generated a patch/diff based some order so will get what is expected. ?
How to handle this kind of scenario ?**

Please help me.

Thank you so much.
~Shyam
BdEngineer
  • 2,929
  • 4
  • 49
  • 85
  • 1
    Interesting problem! A library that I help to maintain (https://github.com/Starcounter-Jack/JSON-Patch) also suffers from this problem: http://jsfiddle.net/warpech/1epzuoj3/ . I think it could be fixed for browsers that natively support `Array.observe`. But for older browsers, expensive dirty checking needs to be made. – warpech Apr 03 '15 at 08:42

1 Answers1

0

We are currently working on this issue in Starcounter-Jack/JSON-Patch.

It seems to work nice with native Array.Observe- http://jsfiddle.net/tomalec/p4s7aw96/.

Try Starcounter-Jack/JSON-Patch issues/65_ArrayObserve branch we will release it as new version once shim and performance will be checked.

Feel free to add you comments at JSON-Patch issue board

tomalec
  • 890
  • 10
  • 27
  • thank you . Probably my question has some ambiguity , let me put it correctly. When I generate diff/patch it shows one node is removed and also showing some other nodes are being replaced , in fact i deleted only one node. This is happening because the patch is not able to preserve or maintain the order of nodes .....so I need some way to let patch generate the diff on certain order so that results would come correctly. – BdEngineer Apr 06 '15 at 07:48
  • @user3252097, this is currently in progress, please check http://jsfiddle.net/tomalec/p4s7aw96/ repo is here https://github.com/tomalec/JSON-Patch/tree/issues/65_ArrayObserve_shim Unfortunately, it was no able to make it work with shimmed `Array.observe`, so it still requires native support. – tomalec Apr 07 '15 at 08:20