Oracle 12c Release 2 has enhanced the JSON Functionality where we can generate the JSON Objects directly from the table data. The following are the functions available.
JSON_OBJECT
The JSON_OBJECT function converts a comma-separated list of key-value pairs into object members within a JSON object.
JSON_OBJECTAGG
The JSON_OBJECTAGG aggregate function creates a single JSON object containing a list of object members formed by aggregating a key-value pair from each row.
JSON_ARRAY
The JSON_ARRAY function converts a comma-separated list of expressions into a JSON array of JSON values.
JSON_ARRAYAGG
The JSON_ARRAYAGG aggregate function, similar to the LISTAGG function, aggregates an expression from each row into a single JSON array.
Complex Example
The Below Query gives a single JSON Array Object contains all the employee & department informations.
Another Example shown below is an SQL query which will return the Employee Details JSON Objects which contains a nested array object called EmployeeInfo. It contains all the employees for a particular department.
The JSON_OBJECT function converts a comma-separated list of key-value pairs into object members within a JSON object.
- SELECT json_object (
- KEY 'EmployeeId' VALUE empno,
- KEY 'EmployeeName' VALUE ename
- ) AS employee
- FROM emp
- ORDER BY empno;
- Output
- ------
- {"EmployeeId":7369,"EmployeeName":"SMITH"}
- {"EmployeeId":7499,"EmployeeName":"ALLEN"}
- {"EmployeeId":7521,"EmployeeName":"WARD"}
- {"EmployeeId":7566,"EmployeeName":"JONES"}
- {"EmployeeId":7654,"EmployeeName":"MARTIN"}
- {"EmployeeId":7698,"EmployeeName":"BLAKE"}
- {"EmployeeId":7782,"EmployeeName":"CLARK"}
- {"EmployeeId":7788,"EmployeeName":"SCOTT"}
- {"EmployeeId":7839,"EmployeeName":"KING"}
- {"EmployeeId":7844,"EmployeeName":"TURNER"}
The JSON_OBJECTAGG aggregate function creates a single JSON object containing a list of object members formed by aggregating a key-value pair from each row.
- SELECT JSON_OBJECTAGG(KEY dname VALUE total_count) FROM (
- SELECT d.dname, count(1) total_count from emp e, dept d
- WHERE e.deptno=d.deptno
- group by d.dname);
- Output
- ------
- {"ACCOUNTING":3,"RESEARCH":5,"SALES":6}
The JSON_ARRAY function converts a comma-separated list of expressions into a JSON array of JSON values.
- SELECT json_array( empno, ename, sal,mgr,e.deptno,dname,loc ) json_array
- FROM emp e, dept d WHERE e.deptno=d.deptno
- ORDER BY empno;
- Output
- ------
- [7369,"SMITH",800,7902,20,"RESEARCH","DALLAS"]
- [7499,"ALLEN",1600,7698,30,"SALES","CHICAGO"]
- [7521,"WARD",1250,7698,30,"SALES","CHICAGO"]
- [7566,"JONES",2975,7839,20,"RESEARCH","DALLAS"]
- [7654,"MARTIN",1250,7698,30,"SALES","CHICAGO"]
- [7698,"BLAKE",2850,7839,30,"SALES","CHICAGO"]
- [7782,"CLARK",2450,7839,10,"ACCOUNTING","NEW YORK"]
- [7788,"SCOTT",3000,7566,20,"RESEARCH","DALLAS"]
- [7839,"KING",5000,10,"ACCOUNTING","NEW YORK"]
- [7844,"TURNER",1500,7698,30,"SALES","CHICAGO"]
- SELECT json_array( json_object('EmployeeId' VALUE empno, 'EmployeeName' VALUE ename
- , 'Salary' VALUE sal, 'Manager' VALUE mgr),
- json_object('DepartmentId' VALUE d.deptno ,'DepartmentName' VALUE d.dname,
- 'Location' VALUE d.loc)
- ) json_array
- FROM emp e, dept d
- WHERE e.deptno=d.deptno
- ORDER BY empno;
- Output
- ------
- [{"EmployeeId":7369,"EmployeeName":"SMITH","Salary":800,"Manager":7902},{"DepartmentId":20,"DepartmentName":"RESEARCH","Location":"DALLAS"}]
- [{"EmployeeId":7499,"EmployeeName":"ALLEN","Salary":1600,"Manager":7698},{"DepartmentId":30,"DepartmentName":"SALES","Location":"CHICAGO"}]
- [{"EmployeeId":7521,"EmployeeName":"WARD","Salary":1250,"Manager":7698},{"DepartmentId":30,"DepartmentName":"SALES","Location":"CHICAGO"}]
- [{"EmployeeId":7566,"EmployeeName":"JONES","Salary":2975,"Manager":7839},{"DepartmentId":20,"DepartmentName":"RESEARCH","Location":"DALLAS"}]
- [{"EmployeeId":7654,"EmployeeName":"MARTIN","Salary":1250,"Manager":7698},{"DepartmentId":30,"DepartmentName":"SALES","Location":"CHICAGO"}]
- [{"EmployeeId":7698,"EmployeeName":"BLAKE","Salary":2850,"Manager":7839},{"DepartmentId":30,"DepartmentName":"SALES","Location":"CHICAGO"}]
- [{"EmployeeId":7782,"EmployeeName":"CLARK","Salary":2450,"Manager":7839},{"DepartmentId":10,"DepartmentName":"ACCOUNTING","Location":"NEW YORK"}]
- [{"EmployeeId":7788,"EmployeeName":"SCOTT","Salary":3000,"Manager":7566},{"DepartmentId":20,"DepartmentName":"RESEARCH","Location":"DALLAS"}]
- [{"EmployeeId":7839,"EmployeeName":"KING","Salary":5000,"Manager":null},{"DepartmentId":10,"DepartmentName":"ACCOUNTING","Location":"NEW YORK"}]
- [{"EmployeeId":7844,"EmployeeName":"TURNER","Salary":1500,"Manager":7698},{"DepartmentId":30,"DepartmentName":"SALES","Location":"CHICAGO"}]
The JSON_ARRAYAGG aggregate function, similar to the LISTAGG function, aggregates an expression from each row into a single JSON array.
- SELECT JSON_ARRAYAGG(e.ename) employee_array
- FROM emp e
- WHERE e.deptno = 20;
- Output
- ------
- ["SMITH","JONES","SCOTT","ADAMS","FORD"]
The Below Query gives a single JSON Array Object contains all the employee & department informations.
- SELECT JSON_ARRAYAGG(json_object(KEY 'EmployeeDetails' VALUE
- json_array(json_object(KEY 'DepartmentInfo' VALUE json_object(
- KEY 'DepartmentId' VALUE department_id, KEY 'DepartmentName'
- VALUE department_name,KEY 'EmployeeInfo' VALUE
- (SELECT json_arrayagg(json_object(KEY 'EmployeeId' VALUE e.employee_id,
- KEY 'FirstName' VALUE e.first_name
- , KEY 'LastName' VALUE e.last_name
- , KEY 'Email' VALUE e.email
- , KEY 'PhoneNumber' VALUE e.phone_number
- ))
- FROM employees e
- WHERE e.department_id=d.department_id)
- )))) ) json FROM departments d
- WHERE ROWNUM<5;
- Output
- ------
- [
- {
- "EmployeeDetails": [
- {
- "DepartmentInfo": {
- "DepartmentId": 10,
- "DepartmentName": "Administration",
- "EmployeeInfo": [
- {
- "EmployeeId": 200,
- "FirstName": "Jennifer",
- "LastName": "Whalen",
- "Email": "JWHALEN",
- "PhoneNumber": "515.123.4444"
- }
- ]
- }
- }
- ]
- },
- {
- "EmployeeDetails": [
- {
- "DepartmentInfo": {
- "DepartmentId": 20,
- "DepartmentName": "Marketing",
- "EmployeeInfo": [
- {
- "EmployeeId": 201,
- "FirstName": "Michael",
- "LastName": "Hartstein",
- "Email": "MHARTSTE",
- "PhoneNumber": "515.123.5555"
- },
- {
- "EmployeeId": 202,
- "FirstName": "Pat",
- "LastName": "Fay",
- "Email": "PFAY",
- "PhoneNumber": "603.123.6666"
- }
- ]
- }
- }
- ]
- },
- {
- "EmployeeDetails": [
- {
- "DepartmentInfo": {
- "DepartmentId": 30,
- "DepartmentName": "Purchasing",
- "EmployeeInfo": [
- {
- "EmployeeId": 114,
- "FirstName": "Den",
- "LastName": "Raphaely",
- "Email": "DRAPHEAL",
- "PhoneNumber": "515.127.4561"
- },
- {
- "EmployeeId": 115,
- "FirstName": "Alexander",
- "LastName": "Khoo",
- "Email": "AKHOO",
- "PhoneNumber": "515.127.4562"
- },
- {
- "EmployeeId": 116,
- "FirstName": "Shelli",
- "LastName": "Baida",
- "Email": "SBAIDA",
- "PhoneNumber": "515.127.4563"
- },
- {
- "EmployeeId": 117,
- "FirstName": "Sigal",
- "LastName": "Tobias",
- "Email": "STOBIAS",
- "PhoneNumber": "515.127.4564"
- },
- {
- "EmployeeId": 118,
- "FirstName": "Guy",
- "LastName": "Himuro",
- "Email": "GHIMURO",
- "PhoneNumber": "515.127.4565"
- },
- {
- "EmployeeId": 119,
- "FirstName": "Karen",
- "LastName": "Colmenares",
- "Email": "KCOLMENA",
- "PhoneNumber": "515.127.4566"
- }
- ]
- }
- }
- ]
- },
- {
- "EmployeeDetails": [
- {
- "DepartmentInfo": {
- "DepartmentId": 40,
- "DepartmentName": "Human Resources",
- "EmployeeInfo": [
- {
- "EmployeeId": 203,
- "FirstName": "Susan",
- "LastName": "Mavris",
- "Email": "SMAVRIS",
- "PhoneNumber": "515.123.7777"
- }
- ]
- }
- }
- ]
- }
- ]
Another Example shown below is an SQL query which will return the Employee Details JSON Objects which contains a nested array object called EmployeeInfo. It contains all the employees for a particular department.
- SELECT json_object(KEY 'EmployeeDetails' VALUE
- json_array(json_object(KEY 'DepartmentInfo' VALUE json_object(
- KEY 'DepartmentId' VALUE department_id, KEY 'DepartmentName'
- VALUE department_name,KEY 'EmployeeInfo' VALUE
- (SELECT json_arrayagg(json_object(KEY 'EmployeeId' VALUE e.employee_id,
- KEY 'FirstName' VALUE e.first_name
- , KEY 'LastName' VALUE e.last_name
- , KEY 'Email' VALUE e.email
- , KEY 'PhoneNumber' VALUE phone_number
- ))
- FROM employees e
- WHERE e.department_id=d.department_id)
- )))) json FROM departments d;
- Output
- ------
- {
- "EmployeeDetails": [
- {
- "DepartmentInfo": {
- "DepartmentId": 20,
- "DepartmentName": "Marketing",
- "EmployeeInfo": [
- {
- "EmployeeId": 201,
- "FirstName": "Michael",
- "LastName": "Hartstein",
- "Email": "MHARTSTE",
- "PhoneNumber": "515.123.5555"
- },
- {
- "EmployeeId": 202,
- "FirstName": "Pat",
- "LastName": "Fay",
- "Email": "PFAY",
- "PhoneNumber": "603.123.6666"
- }
- ]
- }
- }
- ]
- }