我有以下JSON字段:
{
"Id": "64848e27-c25d-4f15-99db-b476d868b575",
"Associations_": [
"RatingBlockPinDatum"
],
"RatingScenarioId": "00572f95-9b81-4f7e-a359-3df06b093d4d",
"RatingBlockPinDatum": [
{
"Name": "mappedmean",
"PinId": "I.Assessment",
"Value": "24.388",
"BlockId": "Score"
},
{
"Name": "realmean",
"PinId": "I.Assessment",
"Value": "44.502",
"BlockId": "Score"
}]}
我想将值从24.388更新为嵌套数组中的新值" RatingBlockPinDatum"其中Name =" mappedmean"。
任何帮助将不胜感激。我已经尝试了这个但是无法使其适应工作:
答案 0 :(得分:0)
您可以先在RatingBlockPinDatum
JSON数组中的每个元素获得一个结果(使用jsonb_array_length
和generate_series
),然后对Name
键所具有的结果进行过滤价值“mappedmean”。然后你有需要更新的记录。更新本身可以使用jsonb_set
:
with cte as (
select id, generate_series(0, jsonb_array_length(info->'RatingBlockPinDatum')-1) i
from mytable
)
update mytable
set info = jsonb_set(mytable.info,
array['RatingBlockPinDatum', cte.i::varchar, 'Value'],
'"99.999"'::jsonb)
from cte
where mytable.info->'RatingBlockPinDatum'->cte.i->>'Name' = 'mappedmean'
and cte.id = mytable.id;
将“99.999”替换为您要在Value
属性中存储的任何值。