尝试根据空手道API测试框架中其他键的值为单个值过滤json对象 [英] Trying to filter json object for a single value based on the value of other key in Karate API Test Framework
问题描述
我认为空手道文档很棒,我已经尝试了尽可能多的阅读,但是这一件事现在让我很困惑.我认为我想做的事情很简单,但是我却失败了.根据文档中的示例,我在这里有一个JSON对象,但稍作修改:
* def cat ="[{名称:"Billie",小猫:[{id:23,昵称:'Bob'},{ID:42,昵称:通配"}]},{名称:"Billie2",小猫:[{id:233,昵称:'Bob2'},{id:422,昵称:'Wild2'}]}]"
当 id
为233时,我只想找到 nickName
的值.(在此示例中,答案为 Bob2
)>
我尝试了这个: get [0] cat.kittens [?(@.id == 233)]
但是我想我缺少了一些东西.
令我震惊的是,有多个 name
和 kitten
,而不是文档网站中给出的示例中的一组.抱歉,答案很简单,但任何朝着正确方向的提示将不胜感激.谢谢!
您很亲密.当涉及数组时,通常需要 [*]
.
* def temp1 = get [0] cats [*].kittens [?(@.id == 233)]*匹配temp1.nickName =='Bob2'
请注意,这也将起作用:
* def temp2 = get [0] cats..kittens [?(@.id == 233)]*匹配temp2.nickName =='Bob2'
I think Karate documentation is great and I have tried to read as much as possible but this one little thing is stumping me right now. I think what I am trying to do is fairly straightforward but I am failing at it miserably. I have a JSON object here based on the example in the docs but slightly modified:
* def cat =
"""
[
{
name: 'Billie',
kittens: [
{ id: 23, nickName: 'Bob' },
{ id: 42, nickName: 'Wild' }
]
},
{
name: 'Billie2',
kittens: [
{ id: 233, nickName: 'Bob2' },
{ id: 422, nickName: 'Wild2' }
]
}
]
"""
All I want to find is the value of nickName
when id
is 233. (In this example the answer is Bob2
)
I tried this:
get[0] cat.kittens[?(@.id==233)]
But I think I am missing something.
What is tripping me is when there are multiple name
and kittens
, as opposed to a single set in the example given in the documentation website.
I apologize as the answer for this is pretty straightforward, but any hint in the right direction will get greatly appreciated. Thank you!
You are close. When there is an array involved, typically [*]
is needed.
* def temp1 = get[0] cats[*].kittens[?(@.id==233)]
* match temp1.nickName == 'Bob2'
Note that this will also work:
* def temp2 = get[0] cats..kittens[?(@.id==233)]
* match temp2.nickName == 'Bob2'
这篇关于尝试根据空手道API测试框架中其他键的值为单个值过滤json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!