1

I am running postgresql 14.4. I have a table with 2 fields:

id   data_product(varchar)
---  ------------
1    [{"product_id":1,"product_code":"A1","distribution_ratio":40},{"product_id":2,"product_code":"A2","distribution_ratio":60}]
.........

I need to move the data to a new table with fields like:

id   product_id   product_code   distribution_ratio
---  ----------   ------------   ------------------
1        1             A1                 40
1        2             A2                 60
.........

1 Answer 1

0

You can use jsonb_to_recordset function as follows:

select id,extracted_data.*
from your_table
join lateral jsonb_to_recordset(data_product) 
as extracted_data ("product_id" int ,"product_code" text,"distribution_ratio" int) on true
1
  • thanks. i found a way. SELECT id, code, status, product_id, product_code, distribution_ratio FROM template_categories tc, json_to_recordset(tc.data_product::json) AS x(product_id int, product_code text, distribution_ratio float); I tried using your way but it didn't work. I am running postgresql.
    – hungtv
    Commented Oct 7, 2022 at 10:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.