[英]How to find all the json key-value pair by matching the value using json query
I have below JSON structure :我有以下 JSON 结构:
{
"key" : "value",
"array" : [
{ "key" : 1 },
{ "key" : 2, "misc": {
"a": "Apple",
"b": "Butterfly",
"c": "Cat",
"d": "Dog"
} },
{ "key" : 3 }
],
"tokenize" : {
"firstkey" : {
"token" : 0
},
"secondkey" : {
"token" : 1
},
"thirdkey" : {
"token" : 0
}
}
}
I am able to traverse the above structure till array->dictionary->b by the below syntax :我能够通过以下语法遍历上述结构直到 array->dictionary->b :
$.array[?(@.key=2)].misc.b
Now I need to print all the tokens which has value 0. The same way as shown above I can traverse till $.array[?(@.key=2)].tokenize.现在我需要打印所有值为 0 的标记。与上面显示的相同,我可以遍历直到 $.array[?(@.key=2)].tokenize。
How can I query it to print all values having token:0 .如何查询它以打印所有具有 token:0 的值。
To be very precise, I want the output to be shown as :准确地说,我希望输出显示为:
[
"tokenize" : {
"firstkey" : {
"token" : 0
},
"thirdkey" : {
"token" : 0
}
}
]
The following query already showing something near to what I want but it does not show the keys ("firstkey" and "thirdkey" in this case).下面的查询已经显示了接近我想要的东西,但它没有显示键(在这种情况下是“firstkey”和“thirdkey”)。
$.tokenize[?(@.token == 0)]
Please help me to get this as well.请帮我也得到这个。
Thanks.谢谢。
You can try this script.你可以试试这个脚本。
$.tokenize[?(@.token == 0)].token
Result:结果:
[
0,
0
]
$.tokenize[?(@.token == 0)]~
will output会输出
[
"firstkey",
"thirdkey"
]
for the OP's sample json, use https://jsonpath-plus.github.io/JSONPath/demo/ to verify against your data.对于 OP 的示例 json,请使用https://jsonpath-plus.github.io/JSONPath/demo/来验证您的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.