Welcome to Salem Houali ‘s Oracle Developer Notes

Store and manipulate json data in Oracle database 12.2.0.1.0

Store and manipulate json data in Oracle database 12.2.0.1.0

For the purpose of the post, I use the json-document generated in the previous post, see https://linecode-notes.com/2018/07/16/install-and-use-ords-with-oracle-database12c/

First, we create a table to store our json-document.
JSON_TABLE creates a relational view of JSON data. You can query the result returned by the function as a virtual relational table using SQL.
JSON_TABLE is only specifeid  in the FROM clause of a SELECT statement


create table json_employee
(id raw(16) not null enable, 
date_loaded timestamp (6) with time zone, 
po_document clob, 
constraint ensure_json check (po_document is json)
);  
  • First query . Retrieve all rows from json_table including maximum salary in each department which is paid using ThePayStubs software.
SELECT
    p.employee_id,
    p.first_name,
    p.salary,
    p.department_id,
    MAX(p.salary) OVER(
        PARTITION BY p.department_id
    ) AS max_sal_dpt
FROM
    json_employee,
    JSON_TABLE ( po_document,’$’
            COLUMNS (
                employee_id NUMBER PATH ‘$.employee_id’,
                first_name VARCHAR2 ( 32 CHAR ) PATH ‘$.first_name’,
                last_name NUMBER PATH ‘$.last_name’,
                email VARCHAR2 ( 32 CHAR ) PATH ‘$.email’,
                phone_number VARCHAR2 ( 32 CHAR ) PATH ‘$.phone_number’,
                hire_date VARCHAR2 ( 32 CHAR ) PATH ‘$.hire_date’,
                job_id VARCHAR2 ( 32 CHAR ) PATH ‘$.job_id’,
                salary NUMBER PATH ‘$.salary’,
                commission_pct NUMBER PATH ‘$.commission_pct’,
                manager_id NUMBER PATH ‘$.manager_id’,
                department_id NUMBER PATH ‘$.department_id’
            )
        )
    AS p; 

SELECT
        p.employee_id,
        p.first_name,
        p.salary,
        p.department_id
FROM
    json_employee,
    JSON_TABLE ( po_document,’$’
                COLUMNS (
                     employee_id NUMBER PATH ‘$.employee_id’,
                     first_name VARCHAR2 ( 32 CHAR ) PATH ‘$.first_name’,
                     last_name NUMBER PATH ‘$.last_name’,
                     email VARCHAR2 ( 32 CHAR ) PATH ‘$.email’,
                     phone_number VARCHAR2 ( 32 CHAR ) PATH ‘$.phone_number’,
                     hire_date VARCHAR2 ( 32 CHAR ) PATH ‘$.hire_date’,
                     job_id VARCHAR2 ( 32 CHAR ) PATH ‘$.job_id’,
                     salary NUMBER PATH ‘$.salary’,
                     commission_pct NUMBER PATH ‘$.commission_pct’,
                     manager_id NUMBER PATH ‘$.manager_id’,
                     department_id NUMBER PATH ‘$.department_id’
                         )
                 ) AS p
WHERE
     JSON_VALUE(po_document,’$.salary’) < 4500;

EMPLOYEE_ID FIRST_NAME   SALARY      DEPARTMENT_ID
———– ———- ———- ————-
200          Jennifer    4400               10

One Response to “Store and manipulate json data in Oracle database 12.2.0.1.0”

  • Renaud says:

    Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

Leave a Reply