I have the following input:
{
"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": {
}
}
I want to get the key of the arrays that has an object which has "value1"
for key: "filterkey1"
. So in this case the output must be:
["subkey1", "subkey3"]
All the elements I care about are in "key1" object.
Get subkey s using keys_unsorted
, and check if their values have filterkey1: "value1"
pair using any
:
.key1 | [
keys_unsorted[] as $k
| if any(.[$k][]; .filterkey1=="value1")
then $k
else empty end
]
if you like to consider alternatives too, here's a unix walk-path
based utility jtc
allowing manipulating JSON similarly:
bash $ <file.json jtc -jw'[key1][filterkey1]:<value1>:[-2]<>k'
[
"subkey1",
"subkey3"
]
bash $
PS> Disclosure: I'm the creator of the jtc
- shell cli tool for JSON operations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.