MyDict
[
{
key1 : abc
key2 : 123
key3 : efg456
}
]
现在,如果我想更改key2的值,即123,我将无法。
我尝试过object_insert(mydict :: variant,'key2',888,true),并丢下了铸造错误。还尝试了array_prepend和array_insert,它们都无法使用。 tia
您可以首先删除键,然后插入
OBJECT_INSERT(OBJECT_DELETE(value, 'key2'), 'key2', 'test_value')
样本查询
select
OBJECT_INSERT(OBJECT_DELETE(value, 'key2'), 'key2', 'test_value') AS flat_updated_dict
from test,
lateral FLATTEN(input => dict)
输出
{
"key1": "abc",
"key2": "test_value",
"key3": "efg456"
}
要将其更改为您的输入格式,您可以使用
ARRAY_AGG
with flat as (
select
OBJECT_INSERT(OBJECT_DELETE(value, 'key2'), 'key2', 'test_value') AS flat_updated_dict
from test,
lateral FLATTEN(input => dict)
)
select
ARRAY_AGG(flat_updated_dict) AS updated_dict
from flat;
输出
[
{
"key1": "abc",
"key2": "test_value",
"key3": "efg456"
}
]