thehme
thehme

Reputation: 2896

Postgresql JSONB object column to Array

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?

Upvotes: 0

Views: 61

Answers (1)

user330315
user330315

Reputation:

Yes, jsonb_build_array() is the right approach:

update the_table
   set the_column = jsonb_build_array(the_column);

Upvotes: 1

Related Questions