0% found this document useful (0 votes)
269 views

Oracle 12c - JSON Functions

The document discusses JSON functions in SQL including JSON_OBJECT, JSON_OBJECTAGG, JSON_ARRAY, and JSON_ARRAYAGG. JSON_OBJECT and JSON_ARRAY create JSON values from query results, while JSON_OBJECTAGG and JSON_ARRAYAGG aggregate multiple records into a single JSON object or array. Examples demonstrate creating JSON representations of employee data from a relational database with department information.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

Oracle 12c - JSON Functions

The document discusses JSON functions in SQL including JSON_OBJECT, JSON_OBJECTAGG, JSON_ARRAY, and JSON_ARRAYAGG. JSON_OBJECT and JSON_ARRAY create JSON values from query results, while JSON_OBJECTAGG and JSON_ARRAYAGG aggregate multiple records into a single JSON object or array. Examples demonstrate creating JSON representations of employee data from a relational database with department information.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

--3.

JSON Functions
/*
JSON - javascript object notation (key-value pair)
The purpose of the JSON function is to extract JSON information from a relational
database

List of functions:
1. JSON_OBJECT: creates a JSON Object
2. JSON_OBJECTAGG: aggregates multiple KEY-VALUE pairs (can be JSON OBJECTS)
into a single JSON OBject
3. JSON_ARRAY: creates the JSON Value of a JSON Object
4. JSON_ARRAYAGG: aggregates multiple arrays into a single one

JSON_OBJECTAGG and JSON_ARRAYAGG are AGGREGATE FUNCTIONS


*/

--JSON_OBJECT
SELECT JSON_OBJECT('ENAME' VALUE nume)
FROM angajati;

SELECT JSON_OBJECT(
'ID_Ang' VALUE id_angajat,
'Nume' VALUE nume
) JSON_OBJ
FROM angajati;

--JSON_OBJECTAGG
SELECT JSON_OBJECTAGG('ENAME' VALUE nume)
FROM angajati;

--Exception Raised (More than 4000 characters returned)


SELECT JSON_OBJECTAGG(
'ENAME' VALUE JSON_OBJECT(
'ID_Ang' VALUE id_angajat,
'Nume' VALUE nume
)
)
FROM angajati;
--Exception Handled by using the JSON_returning_clause
SELECT JSON_OBJECTAGG(
'ENAME' VALUE JSON_OBJECT(
'ID_Ang' VALUE id_angajat,
'Nume' VALUE nume
)
RETURNING VARCHAR2(10000)
)
FROM angajati;

--JSON_ARRAY
SELECT JSON_ARRAY(nume)
FROM angajati;

--JSON_ARRAYAGG
SELECT JSON_ARRAYAGG(nume)
FROM angajati;

--Exercise 1: generate a JSON Object such as the key is the departament name and
the value is an array of employees names from that department
SELECT JSON_OBJECTAGG(
d.denumire_departament VALUE JSON_ARRAYAGG(a.nume)
)
FROM angajati a
INNER JOIN departamente d
ON a.id_departament = d.id_departament
GROUP BY d.denumire_departament;

--Exercise 2: generate a JSON Object such as the key is the departament name and
the value is an array of JSON OBJECTS with contain employees names from that
SELECT JSON_OBJECTAGG(
d.denumire_departament VALUE JSON_OBJECTAGG('ENAME' VALUE a.nume)
) JSON_OBJ
FROM angajati a
INNER JOIN departamente d
ON a.id_departament = d.id_departament
GROUP BY d.denumire_departament;

--Exercise 3
SELECT d.denumire_departament,
JSON_ARRAYAGG(
JSON_OBJECT('ENAME' VALUE a.nume)
) JSON_OBJ
FROM angajati a
INNER JOIN departamente d
ON a.id_departament = d.id_departament
GROUP BY d.denumire_departament;

SELECT JSON_OBJECT(
d.denumire_departament VALUE JSON_ARRAYAGG(
JSON_OBJECT('ENAME' VALUE a.nume)
)
) JSON_OBJ
FROM angajati a
INNER JOIN departamente d
ON a.id_departament = d.id_departament
GROUP BY d.denumire_departament;

You might also like