I have a JSON of nested array that I would like to filter and then sum the size field values for all partitions of a particular topic/prefix. So, for topicA, I want to add the 70, 45 and 100 for topicA-0, topicA-1, topicA-2 sizes to get 215.
{
"version": 1,
"brokers": [
{
"broker": 1,
"logDirs": [
{
"logDir": "/var/lib/kafka/data",
"error": null,
"partitions": [
{
"partition": "topicA-0",
"size": 70,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicB-0",
"size": 40,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicC-0",
"size": 20,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicD-0",
"size": 30,
"offsetLag": 0,
"isFuture": false
}
]
}
]
},
{
"broker": 2,
"logDirs": [
{
"logDir": "/var/lib/kafka/data",
"error": null,
"partitions": [
{
"partition": "topicA-1",
"size": 45,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicB-1",
"size": 50,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicC-1",
"size": 10,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicD-1",
"size": 20,
"offsetLag": 0,
"isFuture": false
}
]
}
]
},
{
"broker": 3,
"logDirs": [
{
"logDir": "/var/lib/kafka/data",
"error": null,
"partitions": [
{
"partition": "topicA-2",
"size": 100,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicB-2",
"size": 60,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicC-2",
"size": 80,
"offsetLag": 0,
"isFuture": false
},
{
"partition": "topicD-2",
"size": 5,
"offsetLag": 0,
"isFuture": false
}
]
}
]
}
]
}
How can I achieve this in jq? I tried jq -c 'select( any(.brokers[].logDirs[].partitions[].partition; test("topicA-")) | .size? | numbers ] | add)' but didn't work. Thanks in advance