21 SQL/JSON Function JSON_QUERY
SQL/JSON function json_query
selects one or more
values from JSON data and returns those values. You can thus use
json_query
to retrieve fragments of a JSON
document.
The JSON data that you query is the first argument to json_query
.
More precisely, it is a SQL expression that returns an instance of a
SQL data type that contains JSON data: type JSON
Foot 1,
VARCHAR2
, CLOB
, or
BLOB
. It can be a table or view column
value, a PL/SQL variable, or a bind variable with proper casting.
The result of evaluating the expression is used as the context
item for evaluating the path expression (described
next).
The second argument to json_query
is a SQL/JSON path expression
followed by optional clauses RETURNING
,
PASSING
, WRAPPER
,
QUOTES
, ON ERROR
, and
ON EMPTY
. The path expression can target
any number of JSON values.
In a path-expression array step, each of the specified positions is matched against the data, in order, no matter how it is specified. The order of array indexes and ranges, multiple occurrences of an index, and duplication of a specified position due to range overlaps all matter.
In the RETURNING
clause you can specify data type JSON
, VARCHAR2
, CLOB
, or BLOB
. A BLOB
result is in the AL32UTF8 character set.
Note:
By default, the default return type (no
RETURNING
clause) for SQL/JSON function
json_query
depends on the input data type:
-
If the input type is
JSON
thenJSON
is also the default return type. -
Otherwise,
VARCHAR2
is the default return type.
But you can use initialization parameter JSON_BEHAVIOR
with a true
value for option
json_query_ret_varchar
, to change the default
return type for the current session to always be VARCHAR2
. (By
default, the option value is false
.)
ALTER SESSION SET JSON_BEHAVIOR = "json_query_ret_varchar:true";
The value returned always contains well-formed JSON data. This includes ensuring that non-ASCII characters in string values are escaped as needed. For example, an ASCII TAB character (Unicode character CHARACTER TABULATION, U+0009) is escaped as \t
. Keywords FORMAT JSON
are not needed (or available) for json_query
— JSON formatting is implicit for the return value.
The wrapper clause determines the form of the returned string value.
If (1) the JSON value to be returned by
json_query
is a string, and (2) the
returning data type is textual, not JSON
type, then
the JSON string-delimiting double-quote characters are
included in the returned value. In this context you
must use keywords OMIT QUOTES
if you want to
omit the delimiting double-quote characters.
For example, if the return type is VARCHAR2
(the
default, if the input data is not of JSON
type),
then the JSON string "hello"
, which has only the
five characters hello
, is returned as the
seven-character VARCHAR2
value
"hello"
.
Note:
You cannot use an array wrapper with json_query
if you
use clause OMIT QUOTES
; a compile-time error is raised if you do
that.
The error clause for json_query
can specify EMPTY ON ERROR
, which means that an empty array ([]
) is returned in case of error (no error is raised).
If initialization parameter compatible
is 20
or greater then Oracle Database supports IETF RFC 8259, which allows a JSON document to contain only a JSON scalar value at top level.
If parameter compatible
is less than 20
then only RFC 4627 is supported. It allows only a JSON object or array, not a scalar, at the top level of a JSON document. RFC 8259 includes support for RFC 4627 (and RFC 7159).
If RFC 8259 is not supported, and if the value targeted by a
json_query
path-expression argument targets
multiple values or a single scalar value, then you must use keywords
WITH WRAPPER
to return the value(s) wrapped
in an array. Otherwise, an error is raised.
If RFC 8259 is supported then json_query
can return scalar JSON
values, by default. To require json_query
to return
only nonscalar JSON values, use keywords DISALLOW
SCALARS
in the RETURNING
clause.
In that case the behavior is the same as if RFC 8259 were not
supported — you must use WITH WRAPPER
.
Example 21-1 shows an example of using
SQL/JSON function json_query
with an array wrapper.
For each document it returns a VARCHAR2
value whose
contents represent a JSON array with elements the phone types, in an
unspecified order. For the document in Example 4-3 the phone types are
"Office"
and "Mobile"
, and
the array returned is either [ "Mobile", "Office" ]
or [ "Office", "Mobile" ]
.
Note that if path expression $.ShippingInstructions.Phone.type
were used in Example 21-1 it would give the same result. Because of SQL/JSON path-expression syntax relaxation, [*].type
is equivalent to .type
.
See Also:
-
Oracle Database SQL Language Reference for information about
json_query
Example 21-1 Selecting JSON Values Using JSON_QUERY
SELECT json_query(data, '$.ShippingInstructions.Phone[*].type'
WITH WRAPPER)
FROM j_purchaseorder;
_________________________________________________________
- JSON_QUERY as JSON_TABLE
SQL/JSON functionjson_query
can be viewed as a special case of functionjson_table
.
Related Topics
- SQL/JSON Path Expression Syntax Relaxation
- RETURNING Clause for SQL Functions
- Wrapper Clause for SQL/JSON Query Functions JSON_QUERY and JSON_TABLE
- Error Clause for SQL Functions and Conditions
- Empty-Field Clause for SQL/JSON Query Functions
- Support for RFC 8259: JSON Scalars
- PASSING Clause for SQL Functions and Conditions
Parent topic: Query JSON Data
Footnote Legend
Footnote 1: Database initialization parametercompatible
must be at least 20 to
use data type JSON
.