Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Aggregation functions

Aggregation functions reduce an array to a single value. A single value counts as an array of one item, and an empty array gives 0 for $sum and $count, and nothing for the others. The items of $sum, $max, $min and $average must be numbers (error T0412).

$sum

$sum(array) adds the numbers of array.

$sum([5, 1, 3, 7, 4])
=> 20
input: invoice.json
$sum(Account.Order.Product.Quantity)
=> 8

$count

$count(array) returns the number of items. It accepts any values.

[$count([1, 2, 3, 1]), $count("hello"), $count([])]
=> [4, 1, 0]
input: address.json
$count(Phone[type = 'office'])
=> 2

$max

$max(array) returns the largest number.

$max([5, 1, 3, 7, 4])
=> 7

$min

$min(array) returns the smallest number.

$min([5, 1, 3, 7, 4])
=> 1
$min([])
=> undefined

$average

$average(array) returns the mean of the numbers.

$average([5, 1, 3, 7, 4])
=> 4

Adapted from the JSONata documentation (MIT license).