JSON wrangling with jq
Help 7 / 10
Summarize data

Sorting and Counting

The next problem I have is that I want to summarize some this JSON data. Each issue returned by GitHub has a collection of labels:

jq '{ title: .title, number: .number, labels: .labels }' issue.json

If I want those labels in alphabetical order I can use the built in sort function. It works like this:

echo '["3","2","1"]' | jq 'sort'

This is similar to how I would sort an array in JavaScript:

const l = ["3", "2", "1"];
l.sort();

Other built-ins that mirror JavaScript functionality are available, like length, reverse, and tostring and they can all be used in a similar way:

echo '["3","2","1"]' | jq 'reverse'
echo '["3","2","1"]' | jq 'length'

If I can combine these built-ins with the selectors I’ve built up so far, I’ll have solved my label sorting problem. So I’ll show that next.

Loading...