请在下面找到我格式化的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]以外的所有内容,如何在空手道中做到这一点?
还有比我目前更好的方法吗?
答案 0 :(得分: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规则的AND节点
您还可以对Scenario Outline:
使用以上任何答案,并从您的Examples:
传递索引/过滤条件
答案 1 :(得分:0)
尝试一下:
#{
#"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