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 use jsonb_build_array, but I'm not completely sure.

Has anyone had to do this before?

1 Answer 1

1

Yes, jsonb_build_array() is the right approach:

update the_table
   set the_column = jsonb_build_array(the_column);
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

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.