简体   繁体   中英

karate : How to set the Json value if object name contains period in an array

Here is my JSON request example code and as u can see #(Paramtervale) as to be replaced by 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

None of the above is working, can anyone please help

Here are 2 different ways, note that for updating a JSON, set is not needed any more. When special characters are in key names, use the square-bracket approach to refer to JSON data:

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

Sample Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM