update object_construct嵌套在雪花中的array_construct中

问题描述 投票:0回答:1
任何人可以帮助我处理这种情况,在此情况下,我可能会在array_construct中嵌套多个object_construct。我无法更新其中一个元素的一个值。我正在使用雪花。这是数据的示例,

MyDict [ { key1 : abc key2 : 123 key3 : efg456 } ]
现在,如果我想更改key2的值,即123,我将无法。
我尝试过object_insert(mydict :: variant,'key2',888,true),并丢下了铸造错误。还尝试了array_prepend和array_insert,它们都无法使用。 tia
    

您可以首先删除键,然后插入
sql json snowflake-cloud-data-platform sql-update data-manipulation
1个回答
0
投票
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"
  }
]

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.