I am trying to convert the following JSON into a csv which has each unique "name" and the total count (i.e: number of times that name appears).
[
{
"name": "test"
},
{
"name": "hello"
},
{
"name": "hello"
}
]
[
{
"name": "hello",
"count": 2
},
{
"name": "test",
"count": 1
}
]
I've tried [.[] | group_by (.name)[] ] but get the following error:
jq: error (at :11): Cannot index string with string "name"
JQ play link: https://jqplay.org/s/fWqNUii1b2
Note, I am already using jq to format the initial raw data into the format above. Please see the JQ play link here: https://jqplay.org/s/PwwRYscmBK
group_by(.name)
| map({name: .[0].name, count: length})
[
{
"name": "hello",
"count": 2
},
{
"name": "test",
"count": 1
}
]
Based on OP's comment, use the following jq filter to count each name across multiple objects, where the .name is nested.
map(.labels)
| map({name: .[0].name, count: length})
Jq▷Play
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