How to get the number of elements in a JSON array stored as CLOB with Oracle 12c?

心已入冬 提交于 2021-02-05 06:27:06

问题


I'm storing a java class A as A_DOC in a clob column in my database.

The structure of A is like:

{

id : 123

var1: abc

subvalues : [{

 id: 1 
 value : a

 },
{
id: 1

value :b
}

...

}
]}

I know I can do things like

select json_query(a.A_DOC, '$.subvalues.value') from table_name a;

and so on, but how I'm looking for a way to count the number of elements in the subvalues array through an sql query. Is this possible?


回答1:


You can use JSON_TABLE:

SELECT
    id, var1, count(sub_id) subvalues
FROM
    JSON_TABLE (
        to_clob('{ id: 123, var1: "abc", subvalues : [{ id: 1, value: "a", }, { id: 2, value: "b" } ]}'),
        '$'
        COLUMNS (
            id NUMBER PATH '$.id',
            var1 VARCHAR PATH '$.var1',
            NESTED PATH '$.subvalues[*]'
            COLUMNS (
                sub_id NUMBER PATH '$.id'
            )
        )
    )
GROUP BY id, var1 



回答2:


the function exists in Oracle 18 only

SELECT json_query('[19, 15, [16,2,3]]','$[*].size()' WITH ARRAY WRAPPER)  FROM dual;

SELECT json_value('[19, 15, [16,2,3]]','$.size()')  FROM dual;


来源:https://stackoverflow.com/questions/38780305/how-to-get-the-number-of-elements-in-a-json-array-stored-as-clob-with-oracle-12c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!