[英]How to join two json path query in json path extractor of jmeter?
Let's say I have this popular data sample. 假设我有这个流行的数据样本。
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
What I want from here is a all the book
where category == fiction
and all the bicycle
where color == red
. 我想从这里得到的是一本所有的
book
,其中category == fiction
和所有bicycle
color == red
。
That is, I want, 那就是我想要的
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
},
"bicycle": {
"color": "red",
"price": 19.95
}
I know, I can use $.store.book[?(@.category == "fiction")]
to achieve the targeted books and $.store.bicycle[?(@.color == 'red')]
to achieve the targeted bicycle. 我知道,我可以使用
$.store.book[?(@.category == "fiction")]
来实现目标书籍和$.store.bicycle[?(@.color == 'red')]
来实现目标自行车。
But how can I achieve both at one go? 但我如何一次性实现这两个目标呢?
You can use &&
and ||
您可以使用
&&
和||
operators. 运营商。
$.store.*[?(@.color== 'red' || @.category == 'fiction')]
will fetch the results as you wanted 将根据需要获取结果
I would recommend implementing your scenario using JSR223 PostProcessor and Groovy language . 我建议使用JSR223 PostProcessor和Groovy语言实现您的场景。 Example code to extract the elements and build a new JSON out of the filtered data will look something like:
从过滤后的数据中提取元素并构建新JSON的示例代码如下所示:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def json = new JsonSlurper().parseText(prev.getResponseDataAsString())
def fictionBooks = json.store.book.findAll {it.category == "fiction"}
def redBikes = json.store.bicycle.findAll{it.color="red"}
JsonBuilder builder = new JsonBuilder(fictionBooks + redBikes)
prev.setResponseData(builder.toPrettyString())
The above code will replace the parent sampler response data with the filtered JSON 上面的代码将使用筛选的JSON替换父采样器响应数据
References: 参考文献:
To return books and bicycle, both you need to : 要退还书籍和自行车,您需要:
$..*[?(@.color== 'red' || @.category == 'fiction')]
For books which have color==red or category==fiction, you will need : 对于颜色为==红色或类别==小说的书籍,您需要:
$.store.*[?(@.color== 'red' || @.category == 'fiction')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.