Join two Json arrays to one with key and value I have two jsonb columns(keys,values).
Eg: keys colum value = [&quot;key1&quot;,&quot;key2&quot;,&quot;key3&quot;,&quot;key4&quot;]
values column = [&quot;val1&quot;,&quot;val2&quot;,&quot;val3&quot;,&quot;val4&quot;]
I want to write a select query to get the output as below based on the array index.
{&quot;key1&quot;:&quot;val1&quot;,&quot;key2&quot;:&quot;val2&quot;,&quot;key3&quot;:&quot;val3&quot;,&quot;key4&quot;:&quot;val4&quot;}
You need to unnest the arrays then aggregate everything back into a single JSON value:
select x.value
from the_table t
  cross join lateral (
    select jsonb_object_agg(k.ky, v.value) as value
    from jsonb_array_elements_text(t.keys) with ordinality as k(ky,idx)
       join jsonb_array_elements(t.values) with ordinality as v(value,idx) on k.idx = v.idx
  ) x

Online example