[jq] Generate every combination from several arrays

Created on November 30, 2026

combinations takes an array of arrays and emits every selection containing one value from each inner array. The example pairs each color with each size.

printf '%s\n' '[["red","blue"],["S","M"]]' | jq -c 'combinations'
["red","S"]
["red","M"]
["blue","S"]
["blue","M"]

-c emits each resulting array on one line.

Collect the results in one array

Wrap the filter in brackets to collect all four combinations in a single result.

printf '%s\n' '[["red","blue"],["S","M"]]' | jq '[combinations]'
[
  [
    "red",
    "S"
  ],
  [
    "red",
    "M"
  ],
  [
    "blue",
    "S"
  ],
  [
    "blue",
    "M"
  ]
]

Reference

jq