Sub topic 5
Turning JSON into rows with JSON_TABLE
Assume you know SELECT, joins, and JSON objects and arrays. This chapter uses Oracle Database 21c or later and assumes an orders table with a JSON column named doc.
JSON_TABLE is a SQL function that converts JSON into relational rows and columns. It is useful when a document contains an array and you need to join, filter, group, or report on each array element.
The row-generating model
Consider this document:
{
"orderId": 1001,
"customer": {"name": "Ada Lovelace"},
"items": [
{"sku": "A-1", "qty": 2, "price": 9.5},
{"sku": "B-7", "qty": 1, "price": 24}
]
}The path $.items[*] means “each element of the items array.” JSON_TABLE creates one relational row for each matching element.
SELECT o.id,
jt.sku,
jt.qty,
jt.price
FROM orders o
CROSS APPLY JSON_TABLE(
o.doc,
'$.items[*]'
COLUMNS (
sku VARCHAR2(20) PATH '$.sku',
qty NUMBER PATH '$.qty',
price NUMBER PATH '$.price'
)
) jt;The result has two rows for this order:
| ID | SKU | QTY | PRICE |
|---|---|---|---|
| 1 | A-1 | 2 | 9.5 |
CROSS APPLY evaluates JSON_TABLE for each row from orders. The JSON table expression can therefore refer to o.doc. Oracle also treats JSON_TABLE as a lateral row source, so it can use columns from the table on its left.
Keeping parent values with NESTED PATH
Use NESTED PATH when you need values from the document root and values from an array in the same result. Parent columns repeat for every array element.
SELECT jt.order_id,
jt.customer_name,
jt.line_no,
jt.sku,
jt.qty
FROM orders o
CROSS APPLY JSON_TABLE(
o.doc,
'$'
COLUMNS (
order_id NUMBER PATH '$.orderId',
customer_name VARCHAR2(100) PATH '$.customer.name',
NESTED PATH '$.items[*]'
COLUMNS (
line_no FOR ORDINALITY,
sku VARCHAR2(20) PATH '$.sku',
qty NUMBER PATH '$.qty'
)
)
) jt;FOR ORDINALITY adds a generated number beginning at 1 for each array element. It is useful when the array order has meaning or when you need to identify a line within an order.
Common errors and choices
| Situation | Result or choice |
|---|---|
| Path points to an object | Define columns for fields inside that object |
| Path points to an array with `[*]` | Produce one row per matching element |
| A field is missing | The column is usually `NULL` |
| A value has the wrong type | Conversion can fail unless you specify error handling |
| Array position matters | Add `FOR ORDINALITY` |
A column definition maps one JSON value to one SQL column. Keep the SQL type compatible with the stored JSON. You can also control missing or invalid values with clauses such as NULL ON ERROR, DEFAULT ... ON ERROR, or ERROR ON ERROR.
Use JSON_TABLE when the array is the unit you need to process. If you only need one scalar value, JSON_VALUE is simpler. If you need to preserve the whole JSON fragment, use JSON_QUERY.
Exercise
What rows does the NESTED PATH '$.items[*]' example produce for an order with three items?
Show answer
It produces three rows. The order ID and customer name repeat, while line_no, sku, and qty contain the values for each item.