我有以下输入内容:
{
"key1": {
"subkey1": [
{
"filterkey1": "value1",
"filterkey2": "value2"
},
{
"filterkey1": "value3",
"filterkey2": "value4"
}
],
"subkey2": [
{
"filterkey1": "value5",
"filterkey2": "value6"
},
{
"filterkey1": "value7",
"filterkey2": "value8"
}
],
"subkey3": [
{
"filterkey1": "value1",
"filterkey2": "value6"
},
{
"filterkey1": "value9",
"filterkey2": "value4"
}
]
},
"key2": {
}
}
我想获取具有对象的数组的键,该对象的键为"value1"
。因此,在这种情况下,输出必须为:
"filterkey1"
我关心的所有元素都在“ key1”对象中。
答案 0 :(得分:4)
使用keys_unsorted
获取子键,并使用filterkey1: "value1"
检查它们的值是否具有any
对:
.key1 | [
keys_unsorted[] as $k
| if any(.[$k][]; .filterkey1=="value1")
then $k
else empty end
]
答案 1 :(得分:0)
如果您也想考虑替代方法,这是一个基于Unix walk-path
的实用程序 jtc
,允许类似地处理JSON:
bash $ <file.json jtc -jw'[key1][filterkey1]:<value1>:[-2]<>k'
[
"subkey1",
"subkey3"
]
bash $
PS>披露:我是jtc
-用于JSON操作的shell cli工具的创建者