Sub topic 7
Generating JSON with JSON_OBJECT and JSON_ARRAYAGG
This chapter assumes basic SQL, joins, and GROUP BY, plus familiarity with JSON objects and arrays. Oracle's SQL/JSON generation functions turn relational rows into documents. JSON_OBJECT creates one JSON object. JSON_ARRAYAGG collects values or objects into one JSON array.
Building one JSON object
Suppose these tables contain departments and employees:
CREATE TABLE department (
department_id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL
);
CREATE TABLE employee (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
department_id NUMBER REFERENCES department (department_id)
);
INSERT INTO department VALUES (10, 'Research');
INSERT INTO department VALUES (20, 'Finance');
INSERT INTO employee VALUES (100, 'Grace', 10);
INSERT INTO employee VALUES (101, 'Alan', 10);
INSERT INTO employee VALUES (200, 'Lin', 20);
COMMIT;Use JSON_OBJECT to map column values to JSON property names:
SELECT JSON_OBJECT(
'employeeId' VALUE e.employee_id,
'name' VALUE e.name,
'department' VALUE d.name
RETURNING CLOB
) AS employee_document
FROM employee e
JOIN department d ON d.department_id = e.department_id
ORDER BY e.employee_id;One result is similar to this:
{"employeeId":100,"name":"Grace","department":"Research"}The VALUE keyword separates a JSON property name from its SQL expression. The property name does not need to match the database column name. RETURNING CLOB is useful when generated documents may be larger than the default VARCHAR2 result.
Collecting rows into an array
JSON_ARRAYAGG is an aggregate function. It combines values from multiple rows into one JSON array. You can aggregate simple values:
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'employeeId' VALUE employee_id,
'name' VALUE name
)
ORDER BY employee_id
RETURNING CLOB
) AS employees
FROM employee;The ORDER BY inside JSON_ARRAYAGG controls the order of elements in the generated array. SQL tables have no inherent row order, so specify it when consumers depend on stable output.
You can also create one nested department document per department. A correlated scalar subquery runs once for each department row:
SELECT JSON_OBJECT(
'departmentId' VALUE d.department_id,
'name' VALUE d.name,
'employees' VALUE COALESCE(
(
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'employeeId' VALUE e.employee_id,
'name' VALUE e.name
)
ORDER BY e.employee_id
RETURNING CLOB
)
FROM employee e
WHERE e.department_id = d.department_id
),
'[]'
) FORMAT JSON
RETURNING CLOB
) AS department_document
FROM department d
ORDER BY d.department_id;FORMAT JSON tells Oracle that the value is already JSON. Without it, the array text could be escaped and stored as a JSON string inside the outer object. COALESCE changes the result for a department with no employees to an empty array, [], instead of a null value.
Choosing the generation function
| Requirement | Function | Result |
|---|---|---|
| Build named properties | `JSON_OBJECT` | One JSON object |
| Build an ordered list | `JSON_ARRAYAGG` | One JSON array |
| Nest generated JSON | `FORMAT JSON` | JSON remains a child value |
The generation functions produce JSON for an API response, export file, view, or later SQL operation. They do not change the relational design underneath.
Exercise
Why should a query use ORDER BY inside JSON_ARRAYAGG when array order matters?
Show answer
Relational rows have no guaranteed order, so the inner ORDER BY makes the order of JSON array elements explicit and repeatable.