如何使用Oracle JSON_VALUE

时间:2020-10-29 23:40:08

标签: sql json oracle stored-procedures json-value

我正在研究扳机。

declare
  v_time number(11, 0);
begin
for i in 0..1
loop
  select JSON_VALUE(body, '$.sections[0].capsules[0].timer.seconds') into v_time from bodycontent where contentid=1081438;
dbms_output.put_line(v_time);
end loop;
end;

但是,索引引用不会变为动态。

like JSON_VALUE(body, '$.sections[i].capsules[i].timer.seconds')

有什么办法可以做到这一点?

3 个答案:

答案 0 :(得分:0)

您需要在json路径中连接变量:

JSON_VALUE(body, '$.sections[' || to_char(i) || '].capsules[0].timer.seconds')

我真的不明白您的问题与触发器之间的关系。

答案 1 :(得分:0)

您可以使用JSON_TABLE

declare
  v_time number(11, 0);
begin
  for i in 0..1 loop
    SELECT time
    INTO   v_time
    FROM   bodycontent b
           CROSS APPLY
           JSON_TABLE(
             b.body,
             '$.sections[*]'
             COLUMNS (
               section_index FOR ORDINALITY,
               NESTED PATH '$.capsules[*]'
               COLUMNS (
                 capsule_index FOR ORDINALITY,
                 time NUMBER(11,0) PATH '$.timer.seconds'
               )
             )
           ) j
    WHERE  j.section_index = i + 1
    AND    j.capsule_index = i + 1
    AND    b.contentid=1081438;

    dbms_output.put_line(v_time);
  end loop;
end;
/

其中,用于测试数据:

CREATE TABLE bodycontent ( body CLOB CHECK ( body IS JSON ), contentid NUMBER );

INSERT INTO bodycontent ( body, contentid ) VALUES (
  '{"sections":[
     {"capsules":[{"timer":{"seconds":0}},{"timer":{"seconds":1}},{"timer":{"seconds":2}}]},
     {"capsules":[{"timer":{"seconds":3}},{"timer":{"seconds":4}},{"timer":{"seconds":5}}]},
     {"capsules":[{"timer":{"seconds":6}},{"timer":{"seconds":7}},{"timer":{"seconds":8}}]}]}',
  1081438
);

输出:

0
4

或者,您可以只使用一个查询:

SELECT section_index, capsule_index, time
FROM   bodycontent b
       CROSS APPLY
       JSON_TABLE(
         b.body,
         '$.sections[*]'
         COLUMNS (
           section_index FOR ORDINALITY,
           NESTED PATH '$.capsules[*]'
             COLUMNS (
               capsule_index FOR ORDINALITY,
               time NUMBER(11,0) PATH '$.timer.seconds'
             )
           )
       ) j
WHERE  ( j.section_index, j.capsule_index) IN ( (1,1), (2,2) )
AND    b.contentid=1081438;

哪个输出:

SECTION_INDEX | CAPSULE_INDEX | TIME
------------: | ------------: | ---:
            1 |             1 |    0
            2 |             2 |    4

(注意:FOR ORDINALITY中的索引比JSON路径中的数组索引高1。)

db <>提琴here

答案 2 :(得分:0)

所以,问题是你想遍历一个索引(对角遍历数组数组,只选取两个元素)——但是 JSON_* 函数不能将变量作为数组索引使用——它们需要硬编码索引。

PL/SQL 对此有一个答案 - 原生动态 SQL,如下所示。

但是,请注意此方法会在同一文档上重复调用 JSON_VALUE()。根据实际要求(我假设您的问题中的那个只是为了说明)和文档的大小,对 JSON_TABLE() 进行一次调用可能更有效,如 MT0 的回答所示。如果实际上您确实只是从一个非常大的文档中提取两个标量值,则可能需要两次调用 JSON_VALUE(),尤其是在文档比此处显示的大得多的情况下;但是,如果您从一个不太复杂的文档中提取许多标量值,那么对 JSON_TABLE() 的单个调用可能会更好,即使最终您没有使用它产生的所有数据。

无论如何 - 作为原生动态 SQL 的说明,这里是替代解决方案(使用 MT0 的表):

declare
  v_time number(11, 0);
begin
  for i in 0..1 loop
    execute immediate q'#
    select json_value(body, '$.sections[#' || i ||
                      q'#].capsules[#' || i || q'#].timer.seconds')
    from   bodycontent
    where  contentid = 1081438#'
    into v_time;
    dbms_output.put_line(v_time);
  end loop;
end;
/


0
4

PL/SQL procedure successfully completed.
相关问题