I have a json that looks like this (it's the result of simple jq filter):
[{
"name": "Corrine",
"firstname": "Odile",
"uid": "PA2685",
"roles": [{
"role_name": "GQ",
"start": "2012-06-20"
},
{
"role_name": "HOUSE",
"start": "2012-06-26"
},
{
"role_name": "HOUSE",
"start": "2017-06-28"
}
]
},
{
"name": "Blanche",
"firstname": "Matthieu",
"uid": "PA2685",
"roles": [{
"role_name": "SENATE",
"start": "2014-06-20"
},
{
"role_name": "SENATE",
"start": "2012-06-26"
},
{
"role_name": "SENATE",
"start": "2012-06-28"
}
]
}
]
I would like to filter in two ways:
role_name (inside the roles array) whose value is HOUSE; start whose date is in 2017 or after.In the json above "Corrine Odile" would be the only one selected.
I tried some with_entries(select(.value expressions, but my I'm confused about how to deal with the dates as well as the "at least one" requirement.
Requirements of the form "at least one" can usually be satisfied efficiently using any/2, e.g.
any(.roles[]; .role_name == "HOUSE")
The check for the year can (apparently) be accomplished by:
.start | .[:4] | tonumber >= 2017
To produce an array of objects satisfying the two conditions:
map(select(
any(.roles[]; .role_name == "HOUSE") and
any(.roles[]; .start[:4] | tonumber >= 2017) ))
.start[:4] is short for .start|.[0:4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With