筛选嵌套JsonPath中的子级
我必须验证以下JSON:筛选嵌套JsonPath中的子级,json,groovy,soapui,jsonpath,rest-assured,Json,Groovy,Soapui,Jsonpath,Rest Assured,我必须验证以下JSON: { "data": { "children": [{ "nodes": [{ "key": "STATE", "value": "Alaska" }], "children": [{ "nodes": [{ "key": "STATE", "value": "Alaska"
{
"data": {
"children": [{
"nodes": [{
"key": "STATE",
"value": "Alaska"
}],
"children": [{
"nodes": [{
"key": "STATE",
"value": "Alaska"
}, {
"key": "NAME",
"value": "Jack"
}, {
"key": "AGE",
"value": "13"
}]
}, {
"nodes": [{
"key": "STATE",
"value": "Alaska"
}, {
"key": "NAME",
"value": "Jill"
}, {
"key": "AGE",
"value": "23"
}]
}]
}, {
"nodes": [{
"key": "STATE",
"value": "Texas"
}],
"children": [{
"nodes": [{
"key": "STATE",
"value": "Texas"
}, {
"key": "NAME",
"value": "John"
}, {
"key": "AGE",
"value": "23"
}]
}]
}]
}
}
JSON中有两个嵌套列表,第一个列表按“状态”分组,它包含包含名称值的子列表。
我只需要找到被归为“阿拉斯加”的名字。
我尝试了以下JsonPath,但它返回null:
$.data..children[?(@nodes.value == 'Alaska')].children[?(@nodes.key=='NAME').value
我还没有尝试过GPath(findAll),但是,如果它是一个更好的解决方案,请给出建议
提前感谢。您可以使用内置的Groovy JsonSlurper来实现这一点。。。假设json是变量
jsonTxt
中的字符串,您只需执行以下操作:
import groovy.json.*
def names = new JsonSlurper().parseText(jsonTxt)
.data
.children
.find { 'Alaska' in it.nodes.value }
.children
.nodes
.flatten()
.findAll { it.key == 'NAME' }
.value
assert names == ['Jack', 'Jill']