我有一个名为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)
仅供学习参考,如有侵权联系我删除