我有一個名為files
的PostgreSQL表,其中包括一個名為formats
的jsonb表。雖然有些行是[null],但其他行具有此結構的對象:
{"thumbnail": {"ext": ".jpg","url": "https://some-url.com/image01.jpg","name": "image01.jpg",//...other properties}
}
對于每一行,我想更新thumbnail.url
,并用other-url
替換some-url
。
解決方案:
我們可以嘗試使用->>
獲取url
的JSON內容值,然后從中替換您的期望值。
因為JSON的url
字段可能是字符串類型,所以在轉換為JSONB
之前,我們需要使用"
將其內容化
jsonb_set(targetjsonb,路徑文本[],new_valuejsonb[,create_missing布爾值])
UPDATE files
SET formats = jsonb_set(formats, '{thumbnail,url}', CONCAT('"',REPLACE(formats->'thumbnail'->>'url','some-url','other-url'),'"')::JSONB);
另外的解決方法:
update public."XXX_DigitalCertificateTemplate"
set "BuildInTemplate" = REPLACE("BuildInTemplate"::text,'} leading to the {Course.CourseAlternativeName}\','}\'
)::jsonb where "Id" in ('xxxxxb57-ca43-dc33-f665-ce9a4052a791','72324e1d-903c-e378-3977-dff3f84xxxxx');
參考鏈接:
https://www.5axxw.com/questions/content/d1y9et
PostgreSQL JSONB 使用入門 - 掘金 (juejin.cn)
僅供學習參考,如有侵權聯系我刪除