Sub topic 6
Flattening Nested Arrays with NESTED PATH
Assume you know SELECT, joins, and JSON objects and arrays. This chapter also assumes an orders table with a native JSON column named doc. Each document contains an orderId and an items array. The examples use Oracle Database 21c or later.
JSON_TABLE turns JSON into relational rows. Its first path selects the part of the document that supplies the rows. A NESTED PATH clause changes the path for a child array while repeating the columns from its parent.
One array: one row per item
Suppose doc contains this structure:
{
"orderId": 1001,
"items": [
{"sku": "A-1", "qty": 2, "price": 9.5},
{"sku": "B-7", "qty": 1, "price": 24}
]
}Use NESTED PATH to create one row for each item:
SELECT jt.order_id,
jt.line_no,
jt.sku,
jt.qty,
jt.price
FROM orders o,
JSON_TABLE(
o.doc, '$'
COLUMNS (
order_id NUMBER PATH '$.orderId',
NESTED PATH '$.items[*]'
COLUMNS (
line_no FOR ORDINALITY,
sku VARCHAR2(20) PATH '$.sku',
qty NUMBER PATH '$.qty',
price NUMBER PATH '$.price'
)
)
) jt;FOR ORDINALITY creates a generated number for each array element. Oracle starts the number at 1. Parent columns, such as order_id, are repeated on every item row.
| JSON_TABLE column | Purpose |
|---|---|
| `order_id` | Reads a value from the order object |
| `line_no` | Numbers items in their array order |
| `sku` | Reads a value from the current item |
| `qty` | Reads a numeric value from the current item |
The expression $.items[*] means “every element in the items array.” Inside that clause, $.sku means the sku field of the current item, not the root order.
Arrays inside array elements
An item can contain another array. For example:
{
"orderId": 1001,
"items": [
{
"sku": "A-1",
"qty": 2,
"options": ["gift-wrap", "express"]
}
]
}Nest another NESTED PATH clause to flatten the options array:
SELECT jt.order_id,
jt.line_no,
jt.sku,
jt.option_no,
jt.option_name
FROM orders o,
JSON_TABLE(
o.doc, '$'
COLUMNS (
order_id NUMBER PATH '$.orderId',
NESTED PATH '$.items[*]'
COLUMNS (
line_no FOR ORDINALITY,
sku VARCHAR2(20) PATH '$.sku',
NESTED PATH '$.options[*]'
COLUMNS (
option_no FOR ORDINALITY,
option_name VARCHAR2(30) PATH '$'
)
)
)
) jt;The final '$' refers to the current option value. If an option is a JSON string, Oracle converts it to VARCHAR2.
Common errors
- Use
[*]when the path identifies an array. Without it, Oracle may try to read the array as one scalar value. - Write paths relative to the current
NESTED PATH. Inside$.items[*],$.skurefers to one item. - Expect null values when an optional child array is missing. Nested columns use outer-join behavior, so the parent can remain visible with null child columns.
- Do not assume array order is a stable business key.
FOR ORDINALITYrecords position, but it does not replace a stored item identifier.
Exercise
An order has three items, and the second item has four options. How many rows can the nested query produce for that order if every item is included even when it has no options?
Show answer
At least six rows: one row for each of the four options on the second item, plus one parent-preserving row for each of the other two items when they have no options.