I have a JSON document as a result of scanning a DVD. It contains a TitleList array, each entry of which contains a ChapterList array, each entry of which has a Duration object. I want to select a property from each TitleList entry where every one of its nested Duration objects matches a condition.
I have been flailing about with various combinations of map(), select() and all() to no avail. Getting into the JSON to filter things is easy, it's getting back out to the upper level to get the Index that is stymieing me.
For those who like to ask "what have you tried" I think this is the closest I've got, but output is empty:
jq '.TitleList[] | select(.ChapterList[] | all(.Duration.Ticks > 180000)) | .Index'
Sample JSON:
{
"TitleList": [
{
"Index": 1,
"ChapterList": [
{
"Name": "Chapter 1",
"Duration": {"Ticks": 48367000}
},
{
"Name": "Chapter 2",
"Duration": {"Ticks": 27470000}
},
{
"Name": "Chapter 3",
"Duration": {"Ticks": 97000}
}
]
},
{
"Index": 2,
"ChapterList": [
{
"Name": "Chapter 1",
"Duration": {"Ticks": 62167000}
},
{
"Name": "Chapter 2",
"Duration": {"Ticks": 58281700}
},
{
"Name": "Chapter 3",
"Duration": {"Ticks": 4582000}
}
]
},
{
"Index": 3,
"ChapterList": [
{
"Name": "Chapter 1",
"Duration": {"Ticks": 48367000}
},
{
"Name": "Chapter 2",
"Duration": {"Ticks": 48281000}
},
{
"Name": "Chapter 3",
"Duration": {"Ticks": 38500}
}
]
}
]
}
I want to return the Index of any entry in the TitleList array where all the Duration.Ticks are > 180000. So with the sample above, the only output should be 2.
In this case, all/2 is your friend:
.TitleList[]
| select( all(.ChapterList[]; .Duration.Ticks > 180000) )
| .Index