PostgreSQL:合并具有相同键的 JSON 对象时,如何附加它们的值?

问题描述 投票:0回答:1

我有两个数组,如下所示,因为我连接了两个数组 -

Array1
中的 ST 键值被
ST
中的
Array2
键值中的值替换,

阵列1:

{"ST": ["Bin", "No", "Comp"], "OSS": ["Class"]}

数组2:

{"ST": ["Pro", "SU"]}" 

预期输出:

{"ST": ["Bin", "No", "Comp","Pro", "SU"], "OSS": ["Class"]}

我如何实现这一目标?

json postgresql merge
1个回答
0
投票
 WITH js AS (
    SELECT
        '{"ST": ["Bin", "No", "Comp"], "OSS": ["Class"]}'::jsonb AS js_data
    )
SELECT
    jsonb_set(js_data, '{ST}', 
    jsonb_path_query_array(js_data, 
    '($.ST[*])') || '["Pro", "SU"]'::jsonb)
FROM
    js
;
                          jsonb_set                           
--------------------------------------------------------------
 {"ST": ["Bin", "No", "Comp", "Pro", "SU"], "OSS": ["Class"]}
© www.soinside.com 2019 - 2024. All rights reserved.