I have table with jsonb
column "Details":
{"Type": "VIP", "Quantity": 1}
I want update column, using query that copies value from "Type"
field to new one "Subscription"
and change value "VIP"->2. Exp below:
{"Type": 2, "Quantity": 1, "Subscription": "VIP"}
I have tried script but received error "Can’t extract scalar value"
Update "Media" set "Details" = jsonb_set("Details", '{Subscription}'::text[],
(select res.value from jsonb_array_elements("Details" -> 'Type') as res), true)
What is wrong? Please, help to find a solution.
2
Answers
Use the concatenation operator:
Test it in db<>fiddle.
According to the documentation:
Thus when you want to modify a value of a key, you can concatenate the key with a new value.
if you have a newwer Version of postgres
||
is the right tools fo r thadb<>fiddle here