JSON_VALUE
The JSON_VALUE function selects a JSON scalar value and returns a SQL scalar value.
Syntax
JSON_VALUE (expr, JSON_basic_path_expression
[JSON_passing_clause] [JSON_value_returning_clause][JSON_query_on_error_clause]
[JSON_query_on_empty_clause] [JSON_query_on_mismatch_clause][TYPE {STRICT|LAX}])expr
expr::= CHAR|NCHAR|VARCHAR2|NVARCHAR2|BLOB|CLOB|NCLOB|JSONThis is the first of two required arguments to the JSON_VALUE function.
The argument is a SQL expression that returns an instance of a scalar SQL data type. The SQL expression accepts as input data types that include the following: CHAR, NCHAR, VARCHAR2, NVARCHAR2, BLOB, CLOB, NCLOB, and JSON.
If expr is null, then JSON_VALUE returns SQL NULL. If expr is not a text literal of well-formed JSON data using strict or lax syntax, then the function returns SQL NULL by default. You can use the JSON_value_on_error_clause to override this default behavior.
JSON_basic_path_expression
This is the second of two required arguments to the JSON_VALUE function.
TimesTen provides SQL access to JSON data using SQL/JSON path expressions. Use this clause to specify a SQL/JSON path expression that targets a single scalar value. The JSON_VALUE function uses the path expression to evaluate expr and find a JSON scalar value that matches or satisfies the path expression. An error occurs if the JSON scalar value does not match or satisfy the path expression.
You can use one or more filter expressions in the path expression to select documents based on their content. If the path expression contains a filter, then the data that matches must also satisfy the filter in order for JSON_VALUE to return the value. The filter expression may refer to SQL/JSON variables, whose values are passed from SQL by binding them with the JSON_passing_clause.
For specific syntax, see JSON_EXISTS Condition in this book. For complete details about syntax and semantics, see SQL/JSON Path Expressions and JSON_VALUE Function in the Oracle TimesTen In-Memory Database JSON Developer's Guide.
JSON_passing_clause
PASSING expr AS identifier [,expr AS identifier]...Use this optional clause to pass values to the path expression. The result of evaluating expr is bound to the corresponding identifier in JSON_basic_path_expression.
JSON_value_returning_clause
RETURNING JSON_value_return_type [ASCII]Use this optional clause to specify the data type of the return value. If you do not specify a returning clause, the default return type is VARCHAR2(4000). For the supported return data types, see JSON_value_return_type.
There are optional clauses and keywords to the RETURNING clause, such as allowing or disallowing scalars, TRUNCATE (if the return type is VARCHAR2 or NVARCHAR2), preserving or truncating time, allowing or not allowing boolean to number conversion, and ASCII. See the following sections for details.
JSON_value_return_type
JSON_value_return_type ::= VARCHAR2[(size[{BYTE|CHAR}]) [TRUNCATE]] | NVARCHAR2[(size) [TRUNCATE]]|
JSON_value_other_char_types|JSON_value_datetime_types|JSON_value_some_numeric_types|JSON_value_additional_typesJSON_value_other_char_types::= CHAR|NCHAR
JSON_value_datetime_types::= {DATE|TIMESTAMP|TT_DATE|TT_TIMESTAMP} [{TRUNCATE|PRESERVE} TIME]
JSON_value_some_numeric_types::= {NUMBER [precision [,scale])]|INTEGER|BINARY_FLOAT|BINARY_DOUBLE} [{ALLOW|DISALLOW} [BOOLEAN] TO NUMBER [CONVERSION]]
JSON_value_additional_types::= BINARY|VARBINARY|CLOB|NCLOB|BOOLEAN|TT_BIGINT|TT_INTEGER|TT_SMALLINT|TT_TINYINTThe JSON_value_return_type clause specifies the supported return types for the RETURNING clause. For example, if you want the return type to be CLOB, specify CLOB.
-
VARCHAR2orNVARCHAR2:When specifying a
VARCHAR2orNVARCHAR2data type elsewhere in SQL, you must specify a size. However, forJSON_VALUE, you can omit the size. In this case,JSON_VALUEreturns a character string of typeVARCHAR2(4000)orNVARCHAR2(4000)respectively.You can specify the optional
TRUNCATEkeyword immediately after theVARCHAR2(size)orNVARCHAR2(size)data type to truncate the return value tosizecharacters. For example, if you specifyVARCHAR2(10)TRUNCATEand the return value is greater than10characters,JSON_VALUEtruncates the value to10characters.If the data type is not large enough to hold the return character string,
JSON_VALUEreturns SQLNULLby default. You can use theJSON_value_on_error_clauseto override this default behavior. -
JSON_value_datetime_types:In your JSON data, you can use a string that represents an ISO 8601 date-with-time value. This indicates it can have a time component. By default, when you use the
RETURNINGclause withJSON_value_datetime_types, and there is a time component,JSON_VALUEtruncates the time component. For theDATE,TIMESTAMP,TT_DATE, andTT_TIMESTAMPreturn data types, you can use theRETURNINGclause with thePRESERVETIMEclause to override this default truncating behavior and preserve the time component. (UsingRETURNINGJSON_value_datetime_typesTRUNCATETIMEhas the same effect asRETURNINGJSON_value_datetime_types.) -
JSON_value_numeric_types: For theNUMBER,INTEGER,BINARY_FLOAT, andBINARY_DOUBLEreturn data types, you can optionally specify the{ALLOW|DISALLOW} [BOOLEAN] TO NUMBER [CONVERSION]clause. You can use this clause when theJSON_VALUEfunction evaluates a path expression to a JSON Boolean. The default isDISALLOW [BOOLEAN] TO NUMBER [CONVERSION].For details, see Using JSON_VALUE with a Boolean JSON Value in the Oracle TimesTen In-Memory Database JSON Developer's Guide.
ASCII
Use this optional keyword to automatically escape any non-ASCII Unicode characters in the return character string by using standard ASCII Unicode escape sequences.
You can use ASCII only with VARCHAR2, NVARCHAR2, and CLOB return data types.
JSON_value_on_error_clause
{NULL | ERROR | DEFAULT literal} ON ERROR-
NULLONERROR: Returns SQLNULLwhen an error occurs. This is the default. -
ERRORONERROR: Returns the appropriate error when an error occurs. -
DEFAULTliteralONERROR: Returnsliteralwhen an error occurs. The data type ofliteralmust match the data type of the value returned by this function.
-
The first argument (
expr) is not well-formed JSON data. -
There is a non-scalar value found when the JSON data is evaluated using the SQL/JSON path expression.
-
There is no match when the JSON data is evaluated using the SQL/JSON path expression. (The path expression is the second argument to
JSON_VALUE.) You can override the behavior for this type of error by specifying theJSON_value_on_empty_clause. -
The data type of the return value is not large enough to hold the return value.
JSON_value_on_empty_clause
{NULL | ERROR | DEFAULT literal} ON EMPTYUse this clause to specify the value returned by this function if no match is found when the JSON data is evaluated using the SQL/JSON path expression. This clause allows you to specify a different outcome for this type of error than the outcome specified with the JSON_value_on_error_clause. For example, if there is a no match found error, and you specify both the JSON_value_on_empty_clause and the JSON_value_on_error_clause, JSON_VALUE uses the JSON_value_on_empty_clause. Otherwise, JSON_VALUE uses the JSON_value_on_error_clause.
-
NULLONEMPTY: Returns SQLNULLwhen there is a no match error. This is the default. -
ERRORONEMPTY: Returns the appropriate error when there is a no match error. -
DEFAULTliteralONEMPTY: Returnsliteralwhen there is a no match error. The data type ofliteralmust match the data type of the value returned by this function.
JSON_value_on_mismatch_clause
({IGNORE|ERROR|NULL} ON MISMATCH [((MISSING DATA)|(EXTRA DATA)|(TYPE ERROR))])...Use this optional clause to control mismatch errors. For complete details and usage, see JSON_VALUE Function in the Oracle TimesTen In-Memory Database JSON Developer's Guide.
Here is a summary:
-
MISSINGDATA: There is no result for the path expression. -
EXTRADATA: There is more data than expected. -
TYPEERROR: There is a data type mismatch.
-
IGNORE: Behaves the same asNULLONMISMATCH. Use theNULLONMISMATCHclause instead. -
ERROR: Returns the appropriate error when a mismatch occurs. NULL: Returns SQLNULLwhen a mismatch occurs.
If you specify ERROR ON MISMATCH or NULL ON MISMATCH, the same error handling applies to all three types of mismatch. The default is NULL ON MISMATCH.
This clause allows you to specify a different outcome for this type of error than the outcome specified with the JSON_value_on_error_clause. For example, if there is a mismatch error, and you specify both the JSON_value_on_mismatch_clause and the JSON_value_on_error_clause, JSON_VALUE uses the JSON_value_on_mismatch_clause. Otherwise, JSON_VALUE uses the JSON_value_on_error_clause.
TYPE {STRICT|LAX}
Use this optional clause to determine if JSON syntax should be STRICT or LAX.
See Also
- JSON_VALUE Function in the Oracle TimesTen In-Memory Database JSON Developer's Guide.