如何验证动态JSON密钥?

时间:2019-04-12 18:18:35

标签: json karate

    {
    "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
         }
      }
     }
    ]
   }
  }

我阅读了文档,但仍不清楚是否可以验证像这样的复杂API。验证该API的演示将帮助我解决其他API验证...尤其是此示例具有动态JSON密钥。如何验证“ 192706”是动态的ADD,DEL和Cont。

1 个答案:

答案 0 :(得分:1)

从文档中,请参阅JsonPath上的文档。还建议您参阅“ JSON转换”部分。这是一种方法:

* 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' }

编辑也可以:

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