我正在尝试从以下示例json响应中有条件地提取元素的值:
{
"books": [
{
"title": "book 1 title",
"author": {
"firstName": "author01",
"lastName": "abc"
}
}
{
"title": "book 2 title",
"author": {
"firstName": "author02",
"lastName": "xyz"
}
}
]
}
我想选择lastName == xyz的书名。这里是我使用的表达式: $ .books [?(@。author.lastName == 'XYZ')]
但它返回[]
由于
答案 0 :(得分:2)
您的JSON路径表达式是正确的,演示:
但是您的JSON存在问题:
{
"books": [
{
"title": "book 1 title",
"author": {
"firstName": "author01",
"lastName": "abc"
}
}, <-----------------------------you need the comma here
{
"title": "book 2 title",
"author": {
"firstName": "author02",
"lastName": "xyz"
}
}
]
}
如果您的回复无法解析为有效的JSON,则必须使用Regular Expression Extractor。如果是 - 请仔细检查您的JSON路径表达式,还值得查看JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios
答案 1 :(得分:2)
尝试以下表达式:
$。书籍[?(@。author.lastName == 'XYZ')。标题