jq add : Sum or Concatenate an Array
jq add combines all elements of an array with the + operator: summing numbers, concatenating strings or arrays, and merging objects.
add is the quick aggregate when a step needs a total, like the sum of artifact sizes from an API listing.
What it does
add applies + across every element of the input array. Numbers are summed, strings and arrays are concatenated, and objects are shallow-merged. add on an empty array returns null.
Common usage
# sum a numeric field across an array
jq '[.[].size] | add' files.json
# total via map
jq 'map(.size) | add' files.json
# concatenate strings
echo '["a","b","c"]' | jq 'add'
# merge an array of objects
jq '.configs | add' settings.jsonFunctions
| Input array of | add returns |
|---|---|
| numbers | Their sum |
| strings | Their concatenation |
| arrays | A single flattened-by-one array |
| objects | A shallow merge (last key wins) |
| empty [] | null |
Common errors in CI
"jq: error: Cannot iterate over null (null)" means add got null, not an array; build the array first with [ ... ]. "jq: error: string (\"5\") and number (3) cannot be added" means the field is a string in the JSON; convert with (.size|tonumber) before summing.