In a table called priceTables
I am storing in a column named value
a JSON object that looks something similar to this:
{
"price": {
"values": {
"tax": 1.59
}
}
}
And would like to run a query to migrate tens of thousands of records to change the name of values
to breakdown
. So it will result in
{
"price": {
"breakdown": {
"tax": 1.59
}
}
}
Any suggestions on the Postgres query for achieving this?
If the name can only occur once in the JSON or if all occurrences should be replaced if there are more, a quick and dirty one is a simple replace()
in the text:
UPDATE elbat
SET value = replace(value::text, '"values"', '"breakdown"')::jsonb;
(Replace jsonb
with json
if the type of the column is json
rather than jsonb
.)
This should work.
UPDATE priceTables
SET value = value || jsonb_build_object(
'price',
jsonb_build_object(
'breakdown',
priceTables.value #> '{price,values}'
)
)
WHERE priceTables.value #> '{price,values}' IS NOT NULL;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.