jq Arrays and Objects/Min and max by field

Find objects with smallest and largest values.

Section: Grouping and Aggregation

Min and max by field

bash
bash
echo '[{"name":"a","age":20},{"name":"b","age":35}]' | jq '{youngest: min_by(.age), oldest: max_by(.age)}'
Explanation

`min_by()` and `max_by()` are very handy for summaries.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Grouping and Aggregation
Count items by field
Build a frequency table from array objects.
OpenIn sheetbashsame section
Sum numeric field
Sum one field across an array of objects.
OpenIn sheetbashsame section
Average numeric field
Compute an average across objects.
OpenIn sheetbashsame section
Partition items by predicate
Split one array into matching and non-matching groups.
OpenIn sheetbashsame section
First array item
Get the first element.
OpenIn sheetbash1 tag match
Object values
Return all object values as an array.
OpenIn sheetbash1 tag match