Friday, February 21, 2020

Comparing object state represented in JSON - A tree comparison techinque in Java for AUD visualization

When persisting data representing as JSON into Mongo DB would be easier, there would also be a good need to compare to object state for changes ( Addition, Updation,Deletion ) AUD, of member/atrributes.

Now comes the next challenge. What if the JSON object has nested attributes or complex java-types.

How will be compare it.
Here are some Java libraries to do the same.

[1]
https://www.baeldung.com/jackson-compare-two-json-objects

[2]

https://stackoverflow.com/questions/50967015/how-to-compare-json-documents-and-return-the-differences-with-jackson-or-gson

https://cassiomolin.com/2018/07/23/comparing-json-documents-in-java/ 

Consider the following JSON documents:
{
  "name": {
    "first": "John",
    "last": "Doe"
  },
  "address": null,
  "birthday": "1980-01-01",
  "company": "Acme",
  "occupation": "Software engineer",
  "phones": [
    {
      "number": "000000000",
      "type": "home"
    },
    {
      "number": "999999999",
      "type": "mobile"
    }
  ]
}
------------------   - - - -- - - - -- - - - - --   
{
  "name": {
    "first": "Jane",
    "last": "Doe",
    "nickname": "Jenny"
  },
  "birthday": "1990-01-01",
  "occupation": null,
  "phones": [
    {
      "number": "111111111",
      "type": "mobile"
    }
  ],
  "favorite": true,
  "groups": [
    "close-friends",
    "gym"
  ]
}
------------------   - - - -- - - - -- - - - - --   
It will produce the following output:
 
Entries only on left
--------------------------
/address: null
/phones/1/number: 999999999
/phones/1/type: mobile
/company: Acme


Entries only on right
--------------------------
/name/nickname: Jenny
/groups/0: close-friends
/groups/1: gym
/favorite: true


Entries differing
--------------------------
/birthday: (1980-01-01, 1990-01-01)
/occupation: (Software engineer, null)
/name/first: (John, Jane)
/phones/0/number: (000000000, 111111111)
/phones/0/type: (home, mobile)
 


Other experts from read ..
Everything was good until I had to compare complex JSON documents, with nested objects and arrays. A JSON document with nested objects is represented as a map of maps and Maps.difference(Map, Map) doesn’t give nice comparison results for that.
So yes this will ease up the problem.

Extension if you were to implement  JSON  Object State Change Notification, you will need to do some more post processing to get to format like

{
   "member_attr_name_1" : { "from_val" : "XYZ","to_val":"ABC"},
   "member_attr_name_2" : { "from_val" : "123","to_val":"789"},
   "member_attr_name_3" : { "from_val" : "2020-02-21T08:37:33+00:00","to_val":"2018-08-21T08:37:33+00:00"},

 ...
}



Analogy : NCS Client of CUCM , DBCNF processing of SQL records change , key functionality at informix level. if it breaks wheww , big things goes at stake.


Related thoughts :
 [1]
You may want to compare two similar mongo collections .
 https://stackoverflow.com/questions/41222805/compare-a-mongo-diff-on-two-collections/41223773

[2]
Pros:
  • you can specify subset of fields to compare.
  • you can see the actual diff of the records.
Cons:
  • to compare the full records, you need to know all possible fields.
  • mongoexport can be slow for huge databases.
To get all fields in all documents in a collection, see this answer.

Above two approaches help in solving set operation problems

[3]

From SQL Result set into JSON 
 

https://stackoverflow.com/questions/6514876/most-efficient-conversion-of-resultset-to-json
https://www.javacodegeeks.com/2018/09/streaming-jdbc-resultset-json.html
http://biercoff.com/nice-and-simple-converter-of-java-resultset-into-jsonarray-or-xml/


 Enjoy :)



1 comment:

  1. reading / parsing JSON documents into java strings

    https://www.codota.com/code/java/methods/javax.json.Json/createReader

    https://www.programcreek.com/java-api-examples/org.json.simple.parser.JSONParser

    ReplyDelete

Note: Only a member of this blog may post a comment.

Followers