Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 511
ankit Avatar asked Aug 05 '16 02:08

ankit


People also ask

Can we store JSON in CLOB?

JSON can be stored as VARCHAR2 (up to 32767 bytes), CLOB and BLOB. CLOB and BLOB have no length limitations.

Does Oracle 12c support JSON?

In addition to the simplified syntax, Oracle Database 12c adds support for SQL/JSON, an extension to the SQL standard that allows the content of JSON documents to be queried as part of a SQL operation.

Is JSON constraint in Oracle?

You can create a table that has JSON columns. You use SQL condition is json as a check constraint to ensure that data inserted into a column is (well-formed) JSON data. Oracle recommends that you always use an is_json check constraint when you create a column intended for JSON data.


1 Answers

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;
like image 51
Alexander Tokarev Avatar answered Sep 30 '22 00:09

Alexander Tokarev