Knight
Knight

Reputation: 59

How do i validate a dynamic JSON key?

    {
    "id": "aghysfgagaw365",
    "MetricLevelId": "4890718305",
    "level": "BUB",
    "type": "Mash",
    "summary": {
    "counts": [
    {},
    {
    "label": {},
    "value": 2674,
    "labelLoc": {
    "192706": {
    "ADD": 8977,
    "DEL": 3257,
    "Count": 59
    },
    "543419": {
    "ADD": 0,
    "DEL": 0,
    "Count": 1
         }
      }
     }
    ]
   }
  }

I read the documentation but i'm still unclear to validate a complex API's like this. a demo to validate this API would help me to solve other API validations...especially this one has a dynamic JSON key.how do i validate ADD,DEL and Cont with "192706" being dynamic.

Upvotes: 1

Views: 384

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

From the docs, please refer to the documentation on JsonPath. Also recommended is the section on "JSON Transforms". Here is one way:

* def response =
"""
{
   "id": "aghysfgagaw365",
   "MetricLevelId": "4890718305",
   "level": "BUB",
   "type": "Mash",
   "summary": {
      "counts": [
         {

         },
         {
            "value": 2674,
            "labelLoc": {
               "192706": {
                  "ADD": 8977,
                  "DEL": 3257,
                  "Count": 59
               },
               "543419": {
                  "ADD": 0,
                  "DEL": 0,
                  "Count": 1
               }
            }
         }
      ]
   }
}
"""
* def label = get[0] response..labelLoc
* def vals = get label.*
* match each vals == { ADD: '#number', DEL: '#number', Count: '#number' }

EDIT also this would work:

* json keys = label.keySet()
* match keys == ['192706', '543419']

Upvotes: 2

Related Questions