[英]How to extract nested elements in JSON response using JSON extractor in JMeter
I would like to extract an element based on a condition inside a nested JSON response.我想根据嵌套 JSON 响应中的条件提取一个元素。 Using the widely searched example below, I would like to extract the storeid of all books authored by "Foo Bar":
使用下面广泛搜索的示例,我想提取“Foo Bar”创作的所有书籍的 storeid:
{
"store": {
"storeId": 1
"book": [
{
"category": "reference",
"author": [
{
"Nigel Rees",
"Foo Bar"
}
],
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": [
{
"Evelyn Waugh"
}
],
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
I have tried using expressions like: $.store[?(@..author=='Foo Bar')].storeId我尝试使用如下表达式: $.store[?(@..author=='Foo Bar')].storeId
I have also tried to see if there are any store returned at all using: $.store[*].book[?(@.author=='Foo Bar')]我还尝试使用以下方法查看是否有任何商店返回: $.store[*].book[?(@.author=='Foo Bar')]
But no result returned... Please help.但没有返回结果......请帮助。 Thanks!
谢谢!
What you produced is not a valid JSON so you won't be able to use JSON Extractor on it.您生成的不是有效的 JSON ,因此您将无法在其上使用 JSON Extractor。
If you amend this good example which you cannot even properly copy and paste to include your storeId
:如果你修改这个你甚至不能正确复制和粘贴以包含你的
storeId
的好例子:
{
"store": {
"storeId": 1,
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"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
}
You will be able to extract the storeId
where one of the authors is Nigel Rees
using the following expression:您将能够使用以下表达式提取其中一位作者是
Nigel Rees
的storeId
:
$.store[?(@.book[?(@.author=="Nigel Rees")])].storeId
Demo:演示:
More information:更多信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.