karate:如果对象名称包含数组中的句点,如何设置Json值

问题描述 投票:2回答:2

这是我的JSON请求示例代码,您可以看到#(Paramtervale)被MyValue取代了

Below is the example of the request json and code:
            * request 
    """
            {
          "query": {
            "bool": {
              "must": [
                {
                  "match_data": {
                    "Event.Data.message": "#(Paramtervale)"
                  }
                },
                {
                  "match_data": {
                    "Event.Name": "#(Paramtervale2)"
                  }
                }
              ],
              "filter": {
                "range": {
                  "@timestamp": {
                    "gte": "now-1m"
                  }
                }
              }
            }
          }
        }
            """
* set request.Event.Data.message = myvalue
* set request.{Event.Data.message} = myvalue
* set request.(Event.Data.message) = myvalue

以上都不起作用,任何人都可以帮忙

json parameter-passing karate
2个回答
1
投票

这里有2种不同的方式,请注意,对于更新JSON,不再需要set。当键名中包含特殊字符时,请使用方括号方法来引用JSON数据:

* def temp = { 'a.b': 'xxx' }
* temp['a.b'] = 'yyy'
* match temp == { 'a.b': 'yyy' }

# using embedded expressions
* def val = 'yyy'
* def temp = { 'a.b': '#(val)' }
* match temp == { 'a.b': 'yyy' }

0
投票

示例代码:

Feature: Validation

    Scenario:
        * def req =
            """
            {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match_data": {
                                    "Event.Data.message": "#(Paramtervale)"
                                }
                            },
                            {
                                "match_data": {
                                    "Event.Name": "#(Paramtervale2)"
                                }
                            }
                        ],
                        "filter": {
                            "range": {
                                "@timestamp": {
                                    "gte": "now-1m"
                                }
                            }
                        }
                    }
                }
            }
            """
        * req.query.bool.must[0].match_data["Event.Data.message"] = "abc"
        * req.query.bool.must[1].match_data["Event.Name"] = "def"
        * print req
© www.soinside.com 2019 - 2024. All rights reserved.