简体   繁体   English

将最后的元素添加到 Oracle 中的对象的 json 数组中

[英]Adding elements in the end to a json array of objects in Oracle

I have a json column named "configuration" in an Oracle database with a data like-我在 Oracle 数据库中有一个名为“configuration”的 json 列,其数据如下 -

{"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"}...]}

How can add elements to the "sections" array inside the CLOB?如何向 CLOB 内的“sections”数组添加元素? for example, add this object to the CLOB- {"active":false, "code":"page.body"}例如,将此对象添加到 CLOB- {"active":false, "code":"page.body"}

I tried to do this-我试图这样做-

 UPDATE *TABLE_NAME*
SET configuration = JSON_MODIFY(configuration, 'append $.sections',JSON_QUERY(N'{"active":false,"code":"page.body"}'))

but I got this error- Error report - SQL Error: ORA-00917: missing comma 00917. 00000 - "missing comma" *Cause:但我收到了这个错误 - 错误报告 - SQL 错误:ORA-00917:缺少逗号 00917。00000 - “缺少逗号” *原因:
*Action: *行动:

Thanks!谢谢!

You can create the function:您可以创建函数:

CREATE FUNCTION json_append_array(
  json  IN CLOB,
  path  IN VARCHAR2,
  value IN CLOB
) RETURN CLOB
IS
  j_obj JSON_OBJECT_T := JSON_OBJECT_T(json);
  j_arr JSON_ARRAY_T  := j_obj.get_Array(path);
BEGIN
  j_arr.append( JSON_OBJECT_T(value) );
  RETURN j_obj.to_Clob();
END;
/

Then you can update the table:然后你可以更新表:

UPDATE TABLE_NAME
SET configuration = JSON_APPEND_ARRAY(
                      configuration,
                      'sections',
                      '{"active":false,"code":"page.body"}'
                    );

Then:然后:

SELECT *
FROM   table_name;

Outputs:输出:

CONFIGURATION配置
{"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"},{"active":false,"code":"page.body"}]} {"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"},{"active":false,"代码":"page.body"}]}

db<>fiddle here db<> 在这里摆弄

You can split the array into rows the use UNION ALL to add another row and re-aggregate and use JSON_MERGEPATCH to update the object:您可以将数组拆分为行,使用UNION ALL添加另一行并重新聚合并使用JSON_MERGEPATCH更新对象:

MERGE INTO table_name dst
USING (
  SELECT t.ROWID AS rid,
         a.new_value
  FROM   table_name t
         CROSS JOIN LATERAL (
           SELECT JSON_OBJECT(
                    KEY 'sections' VALUE JSON_ARRAYAGG(value FORMAT JSON)
                  ) AS new_value
           FROM   (
             SELECT value
             FROM   JSON_TABLE(
                      t.configuration,
                      '$.sections[*]'
                      COLUMNS value CLOB FORMAT JSON PATH '$'
                    )
             UNION ALL
             SELECT EMPTY_CLOB() || '{"active":false,"code":"page.body"}' FROM DUAL
           )
         ) a
) src
ON (dst.ROWID = src.rid)
WHEN MATCHED THEN
  UPDATE
  SET configuration = JSON_MERGEPATCH(dst.configuration, src.new_value);

Which, for the sample data:其中,对于样本数据:

CREATE TABLE table_name (configuration CLOB CHECK (configuration IS JSON));

INSERT INTO table_name ( configuration )
VALUES ('{"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"}]}');

Then, after the merge statement:然后,在合并语句之后:

SELECT *
FROM   table_name;

Outputs:输出:

CONFIGURATION配置
{"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"},{"active":false,"code":"page.body"}]} {"sections":[{"active":true,"code":"page.about"},{"active":true,"code":"page.title"},{"active":false,"代码":"page.body"}]}

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM