21.1 JSON_QUERY as JSON_TABLE

SQL/JSON function json_query can be viewed as a special case of function json_table.

Example 21-2 illustrates the equivalence: the two SELECT statements have the same effect.

In addition to perhaps helping you understand json_query better, this equivalence is important practically, because it means that you can use either function to get the same effect.

In particular, if you use json_query more than once, or you use it in combination with json_exists or json_value (which can also be expressed using json_table), to access the same data, then a single invocation of json_table presents the advantage that the data is parsed only once.

Because of this, the optimizer often automatically rewrites multiple invocations of json_exists, json_value and json_query (any combination) to fewer invocations of json_table.

Note:

You can use SQL hint NO_JSON_TABLE_TRANSFORM to prevent rewriting of multiple invocations of json_exists, json_value and json_query (any combination) to fewer invocations of json_table.

Example 21-2 JSON_QUERY Expressed Using JSON_TABLE

The keywords FORMAT JSON are used only if data_type is not JSON type. (Keywords FORMAT JSON cannot be used with JSON type.)

SELECT json_query(column, json_path
                  RETURNING data_type array_wrapper
                            error_handler ON ERROR)
  FROM table;

SELECT jt.column_alias
  FROM table,
       json_table(column, '$' error_handler ON ERROR
         COLUMNS ("COLUMN_ALIAS" data_type FORMAT JSON array_wrapper
                  PATH json_path)) AS "JT";