如何将JSON_VALUE与变量一起使用

时间:2020-02-15 01:23:11

标签: json oracle

我有一个要在Oracle 18c中构建的语句。以下行可以正常工作:

Select JSON_VALUE(l_resp, '$.items[0].volumeInfo.industryIdentifiers[1].type')
  into l_temp_var
  from dual;

但是,我必须通过变量来改变第二个索引。第二个索引当前包含[1]。我尝试使用[i]定义为数字或varchar,但这不起作用。如何构造Select JSON_VALUE语句,使其使用变量? 感谢您的关注。

2 个答案:

答案 0 :(得分:0)

使用字符串混叠来构建索引字符串。例如:

BEGIN
  FOR i IN 1..10 LOOP
    Select JSON_VALUE(l_resp, '$.items[0].volumeInfo.industryIdentifiers[' || i || '].type')
      into l_temp_var
      from dual;

    -- Do something with the value in l_temp_var here

  END LOOP;
END:

答案 1 :(得分:0)

我无法进行串联。我尝试了另一种方法。我必须将“类型”和“标识符”放入Json表。

    --Obtain the NVP values of "industryIdentifiers" e.g. ISBN_10, ISBN_13 .
    For rowz in 
        (select *
            from json_table(l_resp, '$.items[0].volumeInfo.industryIdentifiers[*]' 
                            columns (ii_type varchar2(512) path '$.type',
                                     ii_identifier varchar2(512) path '$.identifier'
                                     ) 
                            ) j_ii_tab
        )
    Loop
/*
    If rowz.ii_type = 'ISBN_10' Then
        :P133_ISBN_10 := rowz.ii_identifier;
    Elsif rowz.ii_type = 'ISBN_13' Then
        :P133_ISBN_13 := rowz.ii_identifier;
    End If ;
*/    
    dbms_output.put_line('ii_type: ' || rowz.ii_type);
    dbms_output.put_line('ii_identifier: ' || rowz.ii_identifier);
    End Loop rowz;

在以下位置查看Json数据可能会有所帮助:https://www.googleapis.com/books/v1/volumes?q=isbn:9781484204856