Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert data from JSONB type to Array type in PostgreSQL

I have a table with columns in jsonb format which contains data like this:

['A', 'B', 'C']

I want to convert column to array column type but I don’t know how to do it properly.
Example of my script structure:

ALTER TABLE "products" ADD COLUMN "test_array" VARCHAR(2)[];

UPDATE "products" SET test_array = do converting here;

ALTER TABLE "products" DROP COLUMN "test";
ALTER TABLE "products" RENAME COLUMN "test_array" TO "test";

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The syntax looks odd, but this works:

update products 
   set test_array = array(select jsonb_array_elements_text(test));

Working fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading