I’d like to get the count of elements in an json array using jq (without the elements with "name": "current" in the snapshots array).
{
"name": "test1",
"snapshots": [
{
"description": "You are here!",
"name": "current",
"digest": "ccc",
"running": 1
},
{
"description": "You are here!",
"name": "snap1",
"digest": "ddd",
"running": 1
}
]
}
{
"name": "test2",
"snapshots": [
{
"digest": "bbb",
"description": "You are here!",
"name": "current",
"running": 1
},
{
"description": "You are here!",
"name": "snap1",
"digest": "aaa",
"running": 1
},
{
"description": "You are here!",
"name": "snap2",
"digest": "abc",
"running": 1
}
]
}
This is the result I’m looking for (excluding the entries with "name: "current" in the snapshots array):
test1: 1
test2: 2
3
Answers
One way would be to combine string interpolation by writing the filter inside the
".."
demo – jqplay
You may find it useful to define a count function and use that. Using
reduce
can make it a little quicker too for larger inputs.… or use the generic stream-oriented
count/1
function: