SQL/JSON Function JSON_VALUE

SQL/JSON function json_value selects JSON data and returns a SQL scalar or an instance of a user-defined SQL object type or SQL collection type (varray, nested table).

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 it accepts some optional clauses.

The first argument to json_value is a SQL expression that returns an instance of a scalar SQL data type (that is, not an object or collection data type). A scalar value returned from json_value can be of any of these data types: BINARY_DOUBLE, BINARY_FLOAT, BOOLEAN, CHAR, CLOB, DATE, INTERVAL DAY TO SECOND, INTERVAL YEAR TO MONTH, NCHAR, NCLOB, NVARCHAR2, NUMBER, RAWFoot 1, SDO_GEOMETRY, TIMESTAMP, TIMESTAMP WITH TIME ZONE, VARCHAR2, and VECTOR.

If the RETURNING type is VECTOR then the behavior depends on the targeted input data as follows:

Note: In general, if you produce SQL character data of a type other than NVARCHAR2, NCLOB, and NCHAR from a JSON string, and if the character set of that target data type is not Unicode-based, then the conversion can undergo a lossy character-set conversion for characters that can’t be represented in the character set of that SQL type.

The first argument can be a table or view column value, a PL/SQL variable, or a bind variable with proper casting. The result of evaluating the SQL expression is used as the context item for evaluating the path expression.

The second argument to json_value is a SQL/JSON path expression followed by optional clauses RETURNING, PASSING, ON ERROR, ON EMPTY, and ON MISMATCH. The path expression must target a single scalar value, or else an error occurs.

The default error-handling behavior is NULL ON ERROR, which means that no value is returned if an error occurs - an error is not raised. In particular, if the path expression targets a nonscalar value, such as an array, no error is raised, by default. To ensure that an error is raised, use ERROR ON ERROR.

In a path-expression array step, if only one position is specified then it is matched against the data. Otherwise, there is no match (by default, NULL is returned).

Note:

Each field name in a given JSON object is not necessarily unique; the same field name may be repeated. The streaming evaluation that Oracle AI Database employs always uses only one of the object members that have a given field name; any other members with the same field name are ignored. It is unspecified which of multiple such members is used.

See also Unique Versus Duplicate Fields in JSON Objects.


See Also: Oracle AI Database SQL Language Reference for information about json_value

Related Topics


Footnote Legend

Footnote 1: You can use RAW as the return type only when the input data is of JSON data type, and only if the underlying JSON-language scalar type is binary. Otherwise, an error is processed (handled).