简体   繁体   中英

Compare two json structure and get mismatch changes

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?

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

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM