Sub topic 3
Addressing Data with SQL/JSON Path Expressions
This chapter assumes you know SELECT, table aliases, and JSON objects and arrays. It also assumes an orders table with a JSON column named doc, such as the table created in the previous chapter.
A SQL/JSON path expression is a string that tells Oracle where to find data inside a JSON document. A path starts at the document root, written as $.
Reading object fields
Use a dot followed by a field name to move through nested objects:
$.customer.nameThis path means:
- Start at the root.
- Open the
customerobject. - Read its
namefield.
For the document {"customer":{"name":"Ada"}}, the path returns "Ada".
Use bracket notation when a field name contains characters that have a special meaning or when you want to quote the name:
$."order-id"The field name is case-sensitive. $.Customer and $.customer address different fields.
Addressing array elements
Arrays use square brackets. Array indexes start at zero:
$.items[0].skuThis path selects the sku field from the first element of the items array. The second element uses index 1.
You can select every element with [*]:
$.items[*].skuThis path produces the sku value from each item. A path can therefore match one value or several values. The SQL/JSON function that consumes the path determines how Oracle handles the result.
| Path | Meaning |
|---|---|
| `$` | The complete JSON document |
| `$.customer.name` | The nested `name` field |
| `$.items[0]` | The first item in an array |
| `$.items[*].sku` | The `sku` field from every item |
| `$.items[*]?(@.qty > 1)` | Items whose quantity is greater than one |
Filtering array elements
A filter begins with ?() and contains a condition. The @ symbol means “the current array element”:
$.items[*]?(@.qty > 1)Oracle checks each item and keeps the items whose qty value is greater than 1. You can continue through a field after the filter:
$.items[*]?(@.qty > 1).skuThis selects the SKU values for qualifying items.
Use paths with SQL/JSON functions such as JSON_VALUE, JSON_QUERY, JSON_EXISTS, and JSON_TABLE:
SELECT JSON_VALUE(o.doc, '$.customer.name') AS customer_name,
JSON_VALUE(o.doc, '$.items[0].sku') AS first_sku
FROM orders o
WHERE JSON_EXISTS(o.doc, '$.items[*]?(@.qty > 1)');The path in JSON_VALUE should identify a scalar value, such as a string or number. Use a path identifying an object or array with JSON_QUERY, or use it as a condition with JSON_EXISTS.
Common path errors
- Remember that array indexes begin at
0, not1. - Match field names exactly, including capitalization.
- Do not use
$.items.0; array elements require brackets. - Do not assume a missing field is the same as a JSON
nullvalue. - Check whether a path can match multiple values before using
JSON_VALUE. A scalar function is intended for one scalar result.
A path describes location, not data type. The JSON value at that location may still need conversion. For example, a numeric-looking string is different from a JSON number, and SQL/JSON functions provide clauses for controlling the returned SQL type.
> callout note > Keep paths simple and test them against documents containing missing fields, empty arrays, and unexpected field types. These cases often expose assumptions that normal documents hide.
Exercise
What path selects the price field from every element in the items array, and what path selects it only when qty is greater than one?
Show answer
Use $.items[*].price for every price and $.items[*]?(@.qty > 1).price for prices from items whose quantity exceeds one.