如何在jmeter的json路径提取器中连接两个json路径查询?
假设我有这个流行的数据样本如何在jmeter的json路径提取器中连接两个json路径查询?,json,jmeter,jsonpath,Json,Jmeter,Jsonpath,假设我有这个流行的数据样本 { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
{
"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
}
我想从这里得到的是一本所有的书籍
wherecategory==小说
和所有的自行车
wherecolor==红色
就是我要,
{
"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
}
我知道,我可以使用$.store.book[?(@.category==“fiction”)]
来实现目标图书,使用$.store.bicycle[?(@.color==“red”)]
来实现目标自行车
但我怎样才能同时做到这两个呢 您可以使用
&&
和|
运算符
$.store.*[?(@.color='red'| |@.category='france')]
将根据需要获取结果您可以使用
&
和|
运算符
$.store.*[?(@.color='red'| |@.category='france')]
将根据需要获取结果我建议使用和实现您的场景。从过滤数据中提取元素并构建新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())
上面的代码将用过滤后的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())
上面的代码将用过滤后的JSON替换父采样器响应数据
参考资料:
$..*[?(@.color== 'red' || @.category == 'fiction')]
对于颜色==红色或类别==小说的书籍,您需要:
$.store.*[?(@.color== 'red' || @.category == 'fiction')]
要归还书籍和自行车,您需要:
$..*[?(@.color== 'red' || @.category == 'fiction')]
对于颜色==红色或类别==小说的书籍,您需要:
$.store.*[?(@.color== 'red' || @.category == 'fiction')]
我有这个解决方案,但它会在
汽车
和自行车
中搜索颜色
属性和类别
属性,如果汽车
的颜色
是红色
,它也会匹配,但这是不需要的。因此,我想明确检查自行车的颜色是否为红色
或者类别
是否为虚构。我有这个解决方案,但是它将在汽车
和自行车
中搜索颜色
属性和类别
属性,如果汽车
的颜色
为红色
,则也会匹配,但这不是所需的。因此,我想明确检查自行车的颜色
是红色
还是类别
是虚构的。这是一个好问题(与jmeter无关)这是一个好问题(与jmeter无关)