空手道:当我想将值设置为$ .. somewhereInJsonPath时,我得到Path不能以'结尾 [英] Karate: when I want to set value to $..somewhereInJsonPath I get Path must not end with a '

查看:45
本文介绍了空手道:当我想将值设置为$ .. somewhereInJsonPath时,我得到Path不能以'结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新JSON文件中somewhereInJsonPath字段的值.

I want to update a value of somewhereInJsonPath field in my JSON file.

我正在为此:* set myBody $..someWhereInJsonPath = 'AAA'.当我运行测试时,我得到:Path must not end with a '.'

I am using for this: * set myBody $..someWhereInJsonPath = 'AAA'. And when I run test I get: Path must not end with a '.'

但是当我使用* set myBody $..firstHere.someWhereInJsonPath = 'AAA'时,它正在工作.

But when I am using * set myBody $..firstHere.someWhereInJsonPath = 'AAA' it is working.

我认为在第一种情况下,我们要更新$ ..中的第一个值,它也必须起作用.

I think in first case, where we want to update first value in $.., it must working too.

要澄清:

例如,我们有JSON:

For example we have JSON:

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

当我这样做时:$ .store.book [0] .something = 13,它正在工作.

And when I do: $.store.book[0].something = 13 it is working.

但是,当我这样做时:$ .. something = 13,它不起作用.

BUT when I do: $..something = 13 it is not working.

为什么?而我该如何更新呢?

Why? And how can I update this?

当我想查找$ .. something时,在 http://jsonpath.com/上找到它价值.但是,当我在空手道中使用$ .. something时,它将无法正常工作.

At http://jsonpath.com/ when I want to find $..something it is find this value. But when I use $..something in karate it is not working.

已使用 https://stackoverflow进行了设置. com/questions/54928387/karate-jsonpath-wildcards-didnt-work-or-partly-didnt-work

推荐答案

实际上set命令并不是真正为JsonPath通配符设计的.例如,$[*].foo$..foo是通配符的示例.而$[0].foo$.fooresponse.foo是纯JS表达式.

Actually the set command is not really designed for JsonPath wildcards. For example $[*].foo or $..foo are examples of wildcards. And $[0].foo, $.foo or response.foo are pure JS expressions.

因此,请坚持使用这样的纯JS表达式.在下面的情况下,seteval可以互换,这在您要使用动态/变量(例如)时更有用. * eval response[foo]其中foo是字符串.

So please stick to pure JS expressions like this. Here below, set is interchangeable with eval which is more useful in case you want to use dynamic / variables for e.g. * eval response[foo] where foo is a string.

* def response = { foo: { somePath: 'abc' } }
* eval response.foo.somePath = 'xyz'
* match response == { foo: { somePath: 'xyz' } }

如果您确实需要进行批量"更新,请使用转换函数:

If you really do need to do "bulk" updates, use a transform function:

* def response = [{}, {}]
* def fun = function(x, i){ return { foo: 'bar', index: ~~(i + 1) } }
* def res = karate.map(response, fun)
* match res == [{ foo: 'bar', index: 1 }, { foo: 'bar', index: 2 }]

这篇关于空手道:当我想将值设置为$ .. somewhereInJsonPath时,我得到Path不能以'结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆