0

I have two object, one Object A is from the existing production, and another Object B is from our new pipeline. Eventually they both will be serialized into JOSN. Now, I want to compare if these two pipeline generate the same results, which means that if these two object/JSON carry the same set of data.

NOTE THAT, the same means, they have the same schema, and carry the same SET of data. For example,

Object A has

{"person" : [{"name": "alice"}, {"name": "bob"}, {"name" : "mike"}]}, 

and

Object B has

{"person" : [{"name": "bob"}, {"name": "alice"}, {"name" : "mike"}]}. 

This will cause difference if you run JSONDiss something, but in my case, they should be considered as same even if the sequence of key/value pair is difference. The structure of the Object/JSON could be more complex but the idea is that, in each bag/sub-bags, they should contains same set of data.

For my case, the goal is not just find if they are "same" or not, but find what is the difference? for example, Object 3 has a key/value pair called "name" : "Tom" that object 1 does not have. I always and only need to compare two objects.

I am wondering that is there any existing library for doing that ? or is there any suggestion for doing that ?

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Terry
  • 855
  • 4
  • 12
  • 16
  • Why are they considered the same? – Matt Ball Feb 03 '15 at 19:51
  • 1
    @MattBall because collections inside the object are considered as _sets_, as stated in the question. – Dawood ibn Kareem Feb 03 '15 at 19:52
  • Have you tried ordering both arrays then compare the two? – Chris Stillwell Feb 03 '15 at 19:54
  • "Schema" I don't understand. I think you should explain more what constitutes a schema here. Two objects with the same data should compare `equals()` as true, if you have a properly implemented `equals()` method. – markspace Feb 03 '15 at 19:55
  • No library that makes *exactly* what you want. You have to implement `equals()` method on your class, and maybe use SetUtils https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/SetUtils.html#isEqualSet(java.util.Collection, java.util.Collection) from Apache. – fps Feb 03 '15 at 19:55
  • @ChrisS that seems a valid idea, but the schema is complicated, not as simple as the example here. How can I sort it. they are so many levels. – Terry Feb 03 '15 at 19:56
  • Yeah, I'm not OK with the whole "array = set" idea myself. I think it breaks too many rules. Some kind of manual, external comparator seems like it might actually be the best solution. Trying to muck with JSON semantics directly seems like a very bad idea. – markspace Feb 03 '15 at 19:57
  • @markspace Magnamag I can not override the equals since I do not own the object I want to compare. I just want to implement a compare function and generate some reports. – Terry Feb 03 '15 at 19:58
  • So you effectively want to diff json? – PepperBob Feb 03 '15 at 19:59
  • 1
    Then you must use an external comparator. http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html – markspace Feb 03 '15 at 19:59
  • @PepperBob I think JsonDiff tells me more than I want, they even consider the position of the data piece, while I only care about the content. If object a has same set of data that object B has, I consider they are same – Terry Feb 03 '15 at 20:00
  • 2
    Parse the JSON into a POJO of some sort which uses the semantics you want. – Matt Ball Feb 03 '15 at 20:01
  • I know I can mannual pares in the object A and create something like hashset where I put all data in, and then parse the object B and compare with the hashset. but I believe that there must be more efficient and formal way to do it. – Terry Feb 03 '15 at 20:03

1 Answers1

0

I know this is a bit old ... (saw this after)

Here is an answer that should work: Compare JSON with nested arrays and jsons (Array order does not matter)

Community
  • 1
  • 1
Asoub
  • 2,273
  • 1
  • 20
  • 33