Sql Postgres:如何将元素从文本数组列复制到json列中?

Sql Postgres:如何将元素从文本数组列复制到json列中?,sql,json,postgresql,Sql,Json,Postgresql,我有一个文本数组列,现在我想创建一个新的JSON列。在JSON列中,我想从文本数组和当前时间戳创建键值对 例如: 文本数组{aa,bb,cc}->JSON{aa:12:00,bb:12:00,cc:12:00} 如何以这种方式更新整个表中的所有行?假设当前列为命名数据,而新列为新数据,则可以执行以下操作: update the_table set new_data = x.new_data from ( select id, jsonb_object_agg(t.k, to_char(n

我有一个文本数组列,现在我想创建一个新的JSON列。在JSON列中,我想从文本数组和当前时间戳创建键值对

例如:

文本数组{aa,bb,cc}->JSON{aa:12:00,bb:12:00,cc:12:00}


如何以这种方式更新整个表中的所有行?

假设当前列为命名数据,而新列为新数据,则可以执行以下操作:

update the_table
  set new_data = x.new_data
from (
  select id, jsonb_object_agg(t.k, to_char(now(), 'hh24:mi')) as new_data
  from the_table, unnest(data) as t(k)
  group by id
) x
where x.id = foo.id;
在线示例: