【问题标题】:Karate Removing different parts of JSON空手道删除 JSON 的不同部分
【发布时间】:2018-10-16 07:48:58
【问题描述】:

请在下面找到我格式化的 JSON Payload Dis.json

{
  "id": "",
  "discount_type": "COUPON",
  "name": "Festive Treebo discount",
  "code": "ABCDE",
  "description": "50% off discount",
  "is_visible": true,
  "implementation_logic": "EXPLICIT",
  "is_auto_applicable": true,
  "validity_start": "2018-09-30T18:30:00.000Z",
  "validity_end": "2018-10-30T18:30:00.000Z",
  "total_inventory": 100,
  "incentive_value_type": "PERCENTAGE",
  "incentive_apply_on": "TRANSACTION_PRICE",
  "incentive_value": 20,
  "applicable_on": "PRE_TAX",
  "max_incentive_value": 500,
  "rules": {
    "AND": [
      {
        "RULE": {
          "operator": "EQ",
          "attribute_name": "PAYMODE",
          "attribute_value": "PREPAID",
          "attribute_value_type": "STRING",
          "status": "ACTIVE"
        }
      },
      {
        "OR": [
          {
            "AND": [
              {
                "RULE": {
                  "operator": "EQ",
                  "attribute_name": "CHANNEL",
                  "attribute_value": "direct",
                  "attribute_value_type": "CSV_ARRAY",
                  "status": "ACTIVE"
                }
              },
              {
                "RULE": {
                  "operator": "IN",
                  "attribute_name": "SUB_CHANNEL",
                  "attribute_value": "trivago",
                  "attribute_value_type": "CSV_ARRAY",
                  "status": "ACTIVE"
                }
              }
            ]
          },
          {
            "AND": [
              {
                "RULE": {
                  "operator": "EQ",
                  "attribute_name": "CHANNEL",
                  "attribute_value": "assisted-sales",
                  "attribute_value_type": "CSV_ARRAY",
                  "status": "ACTIVE"
                }
              }
            ]
          }
        ]
      },
      {
        "RULE": {
          "operator": "IN",
          "attribute_name": "BLACKOUT_DATES",
          "attribute_value": "16 Oct 2018",
          "attribute_value_type": "CSV_ARRAY",
          "status": "ACTIVE"
        }
      },
      {
        "OR": [
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "HOTEL_ID",
              "attribute_value": "0048880",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          },
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "CITY",
              "attribute_value": "105",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          },
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "LOCALITY",
              "attribute_value": "2",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          }
        ]
      },
      {
        "RULE": {
          "operator": "IN",
          "attribute_name": "DEVICE",
          "attribute_value": "direct-website",
          "attribute_value_type": "CSV_ARRAY",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "EQUALS_GT",
          "attribute_name": "LENGTH_OF_STAY",
          "attribute_value": "3",
          "attribute_value_type": "INTEGER",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "EQUALS_GT",
          "attribute_name": "TRANSACTION_VALUE",
          "attribute_value": "1000",
          "attribute_value_type": "INTEGER",
          "status": "ACTIVE"
        }
      },
      {
        "AND": [
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "UTM_SOURCE",
              "attribute_value": "Paytm",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          },
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "UTM_MEDIUM",
              "attribute_value": "media",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          },
          {
            "RULE": {
              "operator": "IN",
              "attribute_name": "UTM_CAMPAIGN",
              "attribute_value": "new year, holi",
              "attribute_value_type": "CSV_ARRAY",
              "status": "ACTIVE"
            }
          }
        ]
      },
      {
        "RULE": {
          "operator": "EQ",
          "attribute_name": "ABW",
          "attribute_value": "10",
          "attribute_value_type": "INTEGER",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "EQUALS_GT",
          "attribute_name": "CHECKIN",
          "attribute_value": "01 Oct 2018",
          "attribute_value_type": "DATE",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "EQUALS_LT",
          "attribute_name": "CHECKOUT",
          "attribute_value": "16 Nov 2018",
          "attribute_value_type": "DATE",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "IN",
          "attribute_name": "USER_COHORT",
          "attribute_value": "LOYALITY_USER",
          "attribute_value_type": "CSV_ARRAY",
          "status": "ACTIVE"
        }
      },
      {
        "RULE": {
          "operator": "EQUALS_LT",
          "attribute_name": "INVENTORY_CAP",
          "attribute_value": "20",
          "attribute_value_type": "INTEGER",
          "status": "ACTIVE"
        }
      }
    ]
  },
  "status": "ACTIVE",
  "created_by": "abcd@treebohotels.com",
  "success_message": "Coupon applied successfully",
  "error_message": "Coupon not applicable"
}

现在这是我的功能文件代码,它加载 json 文件并尝试只修剪我想要的 json

Scenario: Json formatting
* def discountInput = read('Dis.json')
* remove discountInput.rules.AND[0]
* remove discountInput.rules.AND[2]
* remove discountInput.rules.AND[3]
* remove discountInput.rules.AND[4]
* remove discountInput.rules.AND[5]
* remove discountInput.rules.AND[6]
* remove discountInput.rules.AND[7]
* remove discountInput.rules.AND[8]
* remove discountInput.rules.AND[9]
* remove discountInput.rules.AND[10]
* remove discountInput.rules.AND[11]
* remove discountInput.rules.AND[12]
* print discountInput

当我打印这个时,只有 AND[0] 被删除,但我想删除除 AND[1] 之外的所有内容,我该如何在空手道中做到这一点?

还有比我目前做的更好的方法吗?

【问题讨论】:

  • 我试过 * remove discountInput.rules.AND[0],discountInput.rules.AND[1],discountInput.rules.AND[2] ......但没用
  • 我试过 * remove discountInput.rules.AND[0,2,3,4,5,6,7,8,9..] 没用

标签: json karate


【解决方案1】:

快速回答:

Scenario: Json formatting 
    * def discountInput = read('Dis.json') 
    * def andNode = discountInput.rules.AND[1]
    * remove discountInput.rules
    * set discountInput.rules[0] = andNode

这可能不是一个好的解决方案,我只是想减少你的代码行

编辑:

再来一个答案,

您可以根据提供给 karate.filter 的条件过滤 json,而不是尝试删除数据

Scenario: json filter 
    * def discountInput = read('Dis.json') 
    * def filterCondition = function(x) { if(x.RULE){ return true;}} 
    * def allAND = $discountInput.rules.AND[*]
    * set discountInput.rules = karate.filter(allAND, filterCondition) 

所有符合条件的数据都会从过滤器函数中返回为json数组

从上面的例子中,无论哪个 AND 节点都有一个规则,都会被返回

您还可以将上述任何答案与Scenario Outline: 一起使用,并从您的Examples: 传递索引/过滤条件

【讨论】:

  • 谢谢!我个人认为使用 JS 手动创建 JSON 可能要容易得多!我现在很忙,但试试* if (condition) rules.AND[i] = { blah: '1' }
  • peter,删除不支持表的设置是否正确?
  • 是的,没有要删除的表。这可能是karate.filter() 可以提供帮助的地方
  • Payload 将保持不变,只有规则会改变(在每个场景中,一些规则将从有效负载中删除)所以我想用这种方式实现,而不是通过 JS 构建整个 JSON。
  • 那么上面的答案应该可以正常工作,而不是有多个场景,您可以在示例中使用带有规则索引的场景大纲。
【解决方案2】:

试试这个:

#{
#"RULE": {
#  "operator": "EQUALS_LT",
#  "attribute_name": "CHECKOUT",
#  "attribute_value": "16 Nov 2018",
#  "attribute_value_type": "DATE",
#  "status": "ACTIVE"
#}
#}
Scenario:
* def data = { op: 'EQUALS_LT' }
* set rule1
| path | 0 | 1 |
| RULE.operator | data.op | 'AND' |
| RULE.attribute_name | (null) | 'bbb' |
| RULE.attribute_valule | '16 Nov 2018' | 'ggg' |
| RULE.attribute_value_type | 'DATE' | 'ddd' |
| RULE.status | 'ACTIVE' | 'jjj' |

* print rule1
* set rule2
| path | 0 | 1 |
| RULE.operator | data.op | rule1 |
| RULE.attribute_name | (null) | 'bbb' |
| RULE.attribute_valule | '16 Nov 2018' | 'ggg' |
| RULE.attribute_value_type | 'DATE' | 'ddd' |
| RULE.status | 'ACTIVE' | 'jjj' |

* print rule2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2012-02-23
    相关资源
    最近更新 更多