如何在运行时在空手道中创建和传递或多或少的json负载

时间:2018-10-10 13:05:29

标签: karate

有时我必须传递以下有效载荷

{
   "code": "hhhh",
   "name": "NDS coupon Testing",
   "description": "NDS couopn testing via postman",
   "status": "ACTIVE",
   "comments": null,
   "rules": {
               "AND":[{
                                       "rule":{
                                       "operator" : "EQ",
                                       "status" : "ACTIVE",
                                       "attribute_name" : "HOTEL_ID",
                                       "attribute_value" : "12744",
                                       "attribute_value_type" : "INTEGER"
                                       }
                               }]        
}
}

有时跟随有效载荷

{
   "code": "hhhh",
   "name": "NDS coupon Testing",
   "description": "NDS couopn testing via postman",
   "status": "ACTIVE",
   "comments": null,
   "rules": {
               "AND":[{
                                       "rule":{
                                       "operator" : "EQ",
                                       "status" : "ACTIVE",
                                       "attribute_name" : "HOTEL_ID",
                                       "attribute_value" : "12744",
                                       "attribute_value_type" : "INTEGER"
                                       }
                               }
{

              "AND" : [{"rule" : {

                       "status" : "ACTIVE",
                       "operator" : "EQUALS_LT"
                       "attribute_name" : "checkout",
                       "attribute_value" : "2018-12-31T00:00:00-05:30",
                       "attribute_value_type" : "DATE_TIME"
                      }

                 },]        
}
}

,我的框架结构是 payload.json (具有默认有效负载)。现在在我的功能文件中,我要加载的payload.json set 值需要更新,但是如何在运行时添加/删除json的某些部分呢?

1 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点。为了进一步阅读,我建议以下部分:

如果您花时间确保JSON格式正确,那就太好了。无论如何。

* def json =
"""
{
  "code": "hhhh",
  "name": "NDS coupon Testing",
  "description": "NDS couopn testing via postman",
  "status": "ACTIVE",
  "comments": null,
  "rules": {
    "AND": [
      {
        "rule": {
          "operator": "EQ",
          "status": "ACTIVE",
          "attribute_name": "HOTEL_ID",
          "attribute_value": "12744",
          "attribute_value_type": "INTEGER"
        }
      },
      {
        "rule": {
          "status": "ACTIVE",
          "operator": "EQUALS_LT",
          "attribute_name": "checkout",
          "attribute_value": "2018-12-31T00:00:00-05:30",
          "attribute_value_type": "DATE_TIME"
        }
      }
    ]
  }
}
"""
* eval if (true) karate.remove('json', '$.rules.AND[1]')
* print json