0

I am working with an existing table, where the column is of type JSONB. The data looks like this:

{ key1: {...}, key2: {...}}

I would like to migrate all the existing data in this DB so that this JSONB data looks like this instead:

[{ key1: {...}, key2: {...}}]

I want the existing object to be wrapped in an array.

I think I might be able to usejsonb_build_array, but I'm not completely sure.

Has anyone had to do this before?

askedAug 15, 2022 at 14:58
thehme's user avatar

1 Answer1

1

Yes, jsonb_build_array() is the right approach:

update the_table   set the_column = jsonb_build_array(the_column);
answeredAug 15, 2022 at 15:02
Sign up to request clarification or add additional context in comments.

2 Comments

Great! Can this be easily reverted? I didn't see a way to undo this update, if needed.
Use a transaction and do a rollback if needed

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.