Groovy compare two json with unknown host names and values

I have an API for testing and I have to compare two json answers. Below you can find the file structure. Both files must contain the same elements for comparison, but the order may be different. Unfortunately, the names, type (simple, array), and number of keys (root, nodeXYZ) are also unknown.

{"root": [{
   "node1": "value1",
   "node2": "value1",
   "node3":    [
            {
         "node311": "value311",
         "node312": "value312"
      },
            {
         "node321": "value321",
         "node322": "value322"
      }
   ],
   "node4":    [
            {
         "node411": "value411",
         "node412": "value413",
         "node413": [         {
            "node4131": "value4131",
            "node4132": "value4131"
         }],
         "node414": []
      }
      {
         "node421": "value421",
         "node422": "value422",
         "node423": [         {
            "node4231": "value4231",
            "node4232": "value4231"
         }],
         "node424": []
      }]
   "node5":    [
      {"node51": "value51"},
      {"node52": "value52"},
   ]
}]}

I found useful information in Groovy - compare two JSON objects (same structure) and return an ArrayList containing differences Getting node from Json Response Groovy: how can I find json with a key and find its children in groovy but I could not combine it with the solution . I thought the solution might look like this:

take root
get root children names
check if child has children and get their names
do it to the lowest leve child

( ) , root

+4
2

:

def map1 = new JsonSlurper().parseText(document1)
def map2 = new JsonSlurper().parseText(document2)

assert map1 == map2
+5

JSONassert: https://github.com/skyscreamer/JSONassert. :

JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.STRICT)

, :

java.lang.AssertionError: Resources.DbRdsLiferayInstance.Properties.KmsKeyId
Expected: kms-key-2
     got: kms-key
+1

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


All Articles