Zip array with values from JSON Say I have '{1, 2, 3, 4, 5}' (just an array with the numbers 1-5), and {&quot;1&quot;: 0, &quot;2&quot;: 3, &quot;5&quot;: 0} (comes from a column). With only SQL, is it possible to output this? {&quot;1&quot;: 0, &quot;2&quot;: 3, &quot;3&quot;: 15, &quot;4&quot;: 15, &quot;5&quot;: 0}. The 15 would be like a default value I pull from another column.
Another acceptable output would be an array with the values '{0, 3, 15, 15, 0}'.
Sounds too complex for pure SQL? If not possible, help with a PL/pgSQL would also be appreciated.
Return jsonb object
If there are never additional keys in the JSON object, it can be as simple as:
SELECT jsonb '{&quot;1&quot;: 15, &quot;2&quot;: 15, &quot;3&quot;: 15, &quot;4&quot;: 15, &quot;5&quot;: 15}' || (json-&gt;'key')
FROM   tbl;

Parentheses are required to overrule operator precedence!
This does not remove additional keys from the JSON object.
Returns NULL if the JSON object is not found.
See example in the fiddle.
Consider notes for jsonb concatenation in the manual:

jsonb || jsonb → jsonb
Concatenates two jsonb values. Concatenating two arrays generates an
array containing all the elements of each input. Concatenating two
objects generates an object containing the union of their keys, taking
the second object's value when there are duplicate keys. All other
cases are treated by converting a non-array input into a
single-element array, and then proceeding as for two arrays. Does not
operate recursively: only the top-level array or object structure is merged.

Return int[]
Expanded the JSON object to a set with jsonb_each_text(), RIGHT JOIN to the full set (generated with generate_series() in a CTE), and aggregate with ARRAY constructor (fastest):
WITH a(key) AS (SELECT generate_series(1, 5)::text)
SELECT ARRAY (SELECT COALESCE(value::int, 15)
              FROM   jsonb_each_text(t.json-&gt;'key') RIGHT JOIN a USING (key)
              ORDER  BY key) AS int_arr
FROM   tbl t;

This trims any additional keys from the JSON object.
Returns an array of default values if the JSON object is not found.
See example in the fiddle.
db&lt;&gt;fiddle here
Related:

Why is array_agg() slower than the non-aggregate ARRAY() constructor?