Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a json value greater than 4000 bytes with Oracle 12C JSON_VALUE?

I have a table with a CLOB that stores a big JSON payload. I cannot however select certain attributes whose value is greater than 4000 bytes.

For example, take a json like this:

{
    "foo": "some string smaller than 4k",
    "bar": "some string larger than 4k"
}

The following works:

SELECT json_value(j, '$.foo' ERROR ON ERROR) FROM j;

The following fails with ORA-40478: output value too large (maximum:):

SELECT json_value(j, '$.bar' ERROR ON ERROR) FROM j;

From this 12CR1 documentation:

ORA-40478: output value too large (maximum: string)

Cause: The provided JavaScript Object Notation (JSON) operator generated a result which exceeds the maximum length specified in the RETURN clause.

Action: Increase the maximum size of the data type in the RETURNING clause or use a CLOB or BLOB in the RETURNING clause.

However, using the RETURNING clause fails as well, with ORA-40444: JSON processing error:

SELECT json_value(j, '$.bar' RETURNING CLOB ERROR ON ERROR) FROM j;

It also fails during PLSQL

DECLARE
    val CLOB;
BEGIN
    SELECT json_value(j, '$.bar' RETURNING CLOB ERROR ON ERROR) 
        INTO val 
    FROM j
 END;
like image 696
Matthew Moisen Avatar asked Jul 19 '17 20:07

Matthew Moisen


People also ask

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.

What is Json_value in Oracle?

SQL/JSON function json_value selects a scalar value from JSON data and returns it as a SQL value. You can also use json_value to create function-based B-tree indexes for use with JSON data — see Indexes for JSON Data. Function json_value has two required arguments and accepts optional returning and error clauses.


1 Answers

Json_value function by default will return varchar2(4000), but if you add returning clause with specified varchar2 size you can force it to return much longer values.

In your example the following query should works:

SELECT json_value(j, '$.bar'  returning varchar2(32000) ERROR ON ERROR) FROM j;

PS. In Oracle 12c (SQL) varchar2 is limited to 4000 bytes unless you set MAX_STRING_SIZE parameter to EXTENDED.

32767 bytes or characters if MAX_STRING_SIZE = EXTENDED

like image 76
anytilia Avatar answered Oct 12 '22 09:10

anytilia