Karate 无法为 JSON 设置值

huangapple go评论118阅读模式
英文:

Karate unable to set value for json

问题

我正在使用Karate为负载设置值。我的测试数据之一包含以avro命名空间格式命名的键。对于这种特殊情况,我无法设置该值

  • def payload = { "name": { "com.val.str": "" } }
  • set payload.name["com.val.str"] = 'hello'
  • print payload

错误
```只能添加到数组```

我尝试过多种方式,如
  • set payload.name[com.val.str] = 'hello'
  • set payload.name.["com.val.str"] = 'hello'

然而,都没有起作用。
英文:

I'm using Karate set value for a payload. One of my test data contains a key name in avro namespace format. For this specific case, I'm unable to set the value

* def payload = { "name" : { "com.val.str": ""} }
* set payload.name["com.val.str"] = 'hello'
* print payload

Error
Can only add to an array

I tried multiple things like

* set payload.name[com.val.str] = 'hello'
* set payload.name.["com.val.str"] = 'hello'

However, nothing worked.

答案1

得分: 1

不使用 set,只需记住普通的JS表达式即可:

* def payload = { "name": { "com.val.str": "" } }
* payload.name['com.val.str'] = 'hello'
* match payload == { name: { 'com.val.str': 'hello' } }
英文:

Don't use set, just keep in mind that normal JS expressions work:

* def payload = { "name": { "com.val.str": "" } }
* payload.name['com.val.str'] = 'hello'
* match payload == { name: { 'com.val.str': 'hello' } }

huangapple
  • 本文由 发表于 2023年3月9日 14:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定