Native JSON in PL/SQL: JSON_TABLE & Payload Generation
Relational databases and NoSQL document structures used to be enemies. With Oracle's native JSON support, you can seamlessly shred complex JSON payloads into relational tables and construct nested JSON strings directly from SQL queries.
1. Generating JSON Payloads
When building microservices (e.g., exposing an Oracle Retail API via ORDS), you need to convert relational data into JSON. Forget string concatenation; Oracle provides native generation functions.
Creating JSON Objects and Arrays
JSON_OBJECT(): Creates a key-value JSON object.JSON_ARRAY(): Creates a JSON array.JSON_ARRAYAGG(): Aggregates multiple relational rows into a single JSON array.
Output:
{
"department_id": 10,
"department_name": "Administration",
"employees": [
{"emp_id": 200, "name": "Jennifer Whalen"}
]
}
2. Shredding JSON with JSON_TABLE
If you receive a massive JSON payload from a REST endpoint and need to insert it into relational tables, JSON_TABLE is a lifesaver. It acts as a table function, projecting JSON elements into relational columns.
How it works:
'$'is the root context path.PATH '$.order_id'navigates down the JSON tree to find the value.NESTED PATH '$.lines[*]'iterates through the JSON array, creating a new relational row for every element in the array.
3. Storing and Validating JSON
Prior to Oracle 21c (which introduced the native JSON datatype), JSON was stored in CLOB or VARCHAR2 columns. To ensure bad data isn't inserted, you use the IS JSON check constraint.
Once the constraint is in place, you can query the data using simple dot notation:
SELECT p.payload.order_id, p.payload.customer.name
FROM integration_payloads p;


