Reputation: 75983
Given a document like this:
[
{
"KVs" : [
{
"Key": "animal",
"Value": "lion"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "roger"
},
{
"KVs" : [
{
"Key": "animal",
"Value": "zebra"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "linda"
}
]
I want to use jq
to select only those elements of the top array that contain the KV pair animal == "lion"
.
The output for the above JSON document should be:
{
"KVs" : [
{
"Key": "animal",
"Value": "lion"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "roger"
}
Can't figure out how to accomplish this with select()
. I know how to use it to select based on one specific field. e.g. by key name: .[] | select(.KVs[].Key == "animal")
, right? But how do I tell it to match the same KV object on two fields (Key & Value)?
Upvotes: 11
Views: 8864
Reputation: 75983
NM, solved it with the help of jqplay and some trial and error.
This is the solution:
.[] | select(.KVs[] | .Key == "animal" and .Value == "lion")
Upvotes: 14