Karate: JsonPath wildcards didn't work or part

2020-03-30 07:11发布

JSON file jsonExample:

{
  "store": {
    "book": [
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "something": 12.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}

I want update "something". When I use:

1) * set jsonExample $.store.book[0].something = 13 - it is working

2) * set jsonExample $..book[0].something = 13 - it is working

3) * eval jsonExample.store.book[0].something = 13 - it is working

BUT

1) * set jsonExample $..something = 13 - it is NOT working

2) * eval jsonExample..something = 13 - it is NOT working

I understand that set is not working with wildcards ($[*].foo or $..foo). But are wildcards working with eval? If yes, how? Please example based on file jsonExample above.

1条回答
姐就是有狂的资本
2楼-- · 2020-03-30 08:13

I don't understand why you are so concerned with this. Wildcards will not work for updating JSON. It is that simple.

And one more thing, eval will work with pure JS only. Json-Path is NOT pure JS.

Maybe this will explain it more clearly.

If * set jsonExample $..book[0].something = 13 is working please assume that it is a BUG. Do not rely on it. It may work in this case because the code is resilient as far as possible. But it may not work in other cases or in future versions of Karate.

All the below will work:

* def jsonExample =
"""
{
  "store": {
    "book": [
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "something": 12.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}
"""
# all these will work
* set jsonExample $.store.book[0].something = 13
* match jsonExample.store.book[0].something == 13

* set jsonExample.store.book[0].something = 14
* match jsonExample.store.book[0].something == 14

* eval jsonExample.store.book[0].something = 15
* match jsonExample.store.book[0].something == 15

I really hope this makes it clear !!

查看更多
登录 后发表回答