Sub topic 4
Extracting Values with JSON_VALUE, JSON_QUERY, and JSON_EXISTS
Prerequisites and the extraction model
This chapter assumes basic SQL, table aliases, and JSON objects and arrays. It also assumes an orders table with a JSON column named doc. For example, each document might contain an order number, customer object, status, and items array.
Oracle SQL/JSON functions read a document through a path expression. A path begins with $, which represents the document root. A dot selects an object field, and square brackets select an array element.
Reading scalar values with JSON_VALUE
JSON_VALUE extracts one scalar value, such as a string, number, or date. It cannot return an object or array. Use the RETURNING clause when you want to control the SQL data type.
SELECT JSON_VALUE(o.doc, '$.customer.name') AS customer_name,
JSON_VALUE(o.doc, '$.orderId' RETURNING NUMBER) AS order_id,
JSON_VALUE(o.doc, '$.status') AS status
FROM orders o;The path $.customer.name first selects customer, then selects its name field. If a path does not exist, Oracle normally returns NULL. You can make missing or invalid data fail explicitly:
SELECT JSON_VALUE(
o.doc,
'$.orderId'
RETURNING NUMBER
ERROR ON ERROR
) AS order_id
FROM orders o;Use NULL ON ERROR when bad or missing data should not stop the query. Use ERROR ON ERROR when malformed values indicate a data-quality problem.
Returning objects and arrays with JSON_QUERY
JSON_QUERY returns a JSON object or array rather than a scalar. It is useful when an application needs a nested part of a document.
SELECT JSON_QUERY(o.doc, '$.customer') AS customer_document,
JSON_QUERY(o.doc, '$.items[0]') AS first_item,
JSON_QUERY(o.doc, '$.items') AS all_items
FROM orders o;$.items[0] selects the first array element. Array indexes start at zero. If you want the result to remain valid JSON when the selected value is a scalar, use the DISALLOW SCALARS or WRAPPER options according to the expected result shape. For ordinary object and array extraction, the default behavior is usually sufficient.
Filtering with JSON_EXISTS
JSON_EXISTS tests whether a path, optionally with a condition, matches the document. It is commonly used in a WHERE clause.
SELECT o.id,
JSON_VALUE(o.doc, '$.customer.name') AS customer_name
FROM orders o
WHERE JSON_EXISTS(o.doc, '$.items[*]?(@.qty > 1)');The [*] examines every element in items. The filter ?(@.qty > 1) keeps documents containing at least one item whose quantity is greater than one. Unlike JSON_VALUE, JSON_EXISTS is a test, not a way to return the matching item.
| Need | Function | Result |
|---|---|---|
| Read `status` | `JSON_VALUE` | SQL scalar |
| Return `customer` | `JSON_QUERY` | JSON object |
| Test for a matching item | `JSON_EXISTS` | Predicate |
Dot notation and common errors
Oracle also supports simplified dot notation for JSON columns:
SELECT o.doc.status AS status,
o.doc.customer.name AS customer_name
FROM orders o
WHERE JSON_EXISTS(o.doc, '$.items[*]');Always use a table alias with dot notation. Explicit functions are often clearer when you need a return type, error handling, or a complex path.
A frequent error is using JSON_VALUE for an object or array. That produces an error or NULL, depending on the error settings. Another error is forgetting that an absent path is different from a present JSON null; choose error behavior deliberately when that distinction matters.
Exercise
Which function should you use to return the items array from o.doc, and which function should you use to find orders containing an item with qty equal to 3?
Show answer
Use JSON_QUERY(o.doc, '$.items') to return the array. Use JSON_EXISTS(o.doc, '$.items[*]?(@.qty == 3)') to test for a matching item.