2

Hi I have json structure #1 and #2 as follows. I would like to compare and capture the results.

Json #1.

{
    "menu": {
        "id": "file",
        "popup": {
            "menuitem": {
                "menuitem-1": "sometext",
                "menuitem-2": {
                    "menuitem-2.1": "sometext",
                    "menuitem-2.2": "sometext",
                    "menuitem-2.3": {
                        "menuitem-2.3.1": "sometext"
                    }
                }
            }
        },
        "value": "File"
    }
}

Json #2

{
    "menu": {
        "id": "file",
        "popup": {
            "menuitem": {
                "menuitem-2.3": {
                    "menuitem-2.3.1": "sometext"
                }
                "menuitem-1": "sometext",
                "menuitem-2": {
                    "menuitem-2.1": "sometext",
                    "menuitem-2.2": "sometext"
                },
            }
        },
        "value": "File"
    }
}

Am expecting that below JSON has been moved up in JSON #2. My goal here is identify any CREATE NEW / UPDATE / ADJUSTED / DELETE on JSON#2.

"menuitem-2.3": {
   "menuitem-2.3.1": "sometext"
}

Is there any Spring / Java existing framework available to achieve above?

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Sabarish.K
  • 109
  • 1
  • 2
  • 7
  • 2
    Is it possible to create a java POJO by Jackson and then just compare 2 classes? – dehasi Aug 28 '18 at 14:41
  • @dehasi yes it is – Casper Aug 28 '18 at 14:47
  • did you look at https://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java?rq=1 might solve your issue. – sticky_elbows Aug 28 '18 at 14:51
  • I've used https://code.google.com/archive/p/java-diff-utils/ in the past for finding the minimal set of INSERT/DELETE/MOVE of nodes in XML trees (which is similar to JSON). The simple approach could be to compare the formatted text output of the JSON. A real "minimal difference" is not as easy as it sounds on trees, certainly not as easy as on lists (or lines of text). – geert3 Aug 28 '18 at 14:54
  • What is the difference between UPDATE and AJUSTED? – samabcde Aug 28 '18 at 15:27

2 Answers2

-1

Try using Apache drill. It is easy to install and supports querying JSON. You can then execute a minus query and get the difference.

You can also query drill using java. Apache drill has a JDBC driver for that.

Hope it helps. :)

Rinkal Rohara
  • 232
  • 1
  • 7
-1

Use difference from org.apache.commons.lang.StringUtils. Compares two Strings, and returns the portion where they differ. (More precisely, return the remainder of the second String, starting from where it's different from the first.)

For example,

difference("i am a machine", "i am a robot") -> "robot".
StringUtils.difference(null, null) = null
StringUtils.difference("", "") = ""
StringUtils.difference("", "abc") = "abc"
StringUtils.difference("abc", "") = ""
StringUtils.difference("abc", "abc") = ""
StringUtils.difference("ab", "abxyz") = "xyz"
StringUtils.difference("abcde", "abxyz") = "xyz"
StringUtils.difference("abcde", "xyz") = "xyz"

Parameters: str1 - the first String, may be null str2 - the second String, may be null