0

I have a text array column and now I would like to create a new JSON column. In JSON column I would like to create key-value pairs from text array and current time stamp.

Example:

Text array {"aa", "bb", "cc"} -> JSON {"aa":"12:00", "bb":"12:00", "cc":"12:00"}

How do I update this way all rows in the whole table?

1 Answer 1

1

Assuming the current column is named data and the new column is new_data you can do something like this:

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;

Online example: https://rextester.com/SMBH72985

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.