Groovy - compare two JSON objects (same structure) and return an ArrayList containing the differences

I have two JSON objects of the same structure - the original and the one with which I want to compare it. There is only one level of information (i.e. no one has children).

I want to compare these two and remember which fields, if any, were different, storing the key in the second array of DifferingFields ArrayList.

    def secondDifferingFields = new ArrayList()
    orignialJSON.each {
        print "original:"+it.value+":VS:"+secondJSON.it+":second"
        if (it.value != secondJSON.it){
                secondDifferingFields.add(it)
        }
    }

I see that I am repeating the values ​​of the original JSON, but not sure how to access the same key value (if this is the correct wording) in the second JSON in order to be able to compare them. With the print line I always see

Original: XYZ: VS: Zero: Second

+1
source share
1

secondJSON.it (it String, , null).

:

secondJSON.get(it.key)
// or secondJSON."$it.key"

. , containsKey, , null .

groovy .

assert [:].it==null // key `String it` does not exist
assert [:].SevenDrunkenNights==null // same here
assert [it:1].it==1 // now it does
assert [:].get('it')==null // same same for `get`
assert [it:1].get('it')==1
+2

Source: https://habr.com/ru/post/1613623/


All Articles