Karate: JsonPath wildcards didn't work or partly didn't work

醉酒当歌 提交于 2019-12-04 02:23:55

问题


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:


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 !!



来源:https://stackoverflow.com/questions/54928387/karate-jsonpath-wildcards-didnt-work-or-partly-didnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!