Parse JSON data in Oracle Database using JSON_TABLE in SQL

Recently, I got opportunity to work on JSON data in Oracle Database. We were receiving this JSON data from Cisco Webex APIs, and the JSON data was complex (nested with json array). With this blog, I am sharing simple example how to parse complex JSON data in Oracle Database using SQL.

For this example, I am using SCOTT schema, and my current database version is Oracle 12.1 which was release in July 2013. With Current versions of Oracle 18c and 19c and Oracle Autonomous Databases, there has been lot of enhancements to parse JSON data. Anyways, let us first create DEPT_JSON table, which will have JSON_VALUE column of CLOB type. One JSON_VALUE field will store complete information of a department and employees data of that department in JSON format.

ngarg> CREATE TABLE DEPT_JSON
  2  (
  3  ID NUMBER,
  4  JSON_VALUE clob
  5  );
Table created.

ngarg> INSERT INTO DEPT_JSON
  2  VALUES(10,
  3   '{"DEPTNO": "10",
  4     "DNAME": "ACCOUNTING",
  5     "EMPLOYEES" : [{ EMPNO: 7839, "ENAME": "KING", SAL: 5000, "JOB": "PRESIDENT"},
  6                    { EMPNO: 7782, "ENAME": "CLARK", SAL: 2450, "JOB": "MANAGER"},
  7                    { EMPNO: 7934, "ENAME": "MILLER", SAL: 1300, "JOB": "CLERK"}
  8  ]
  9   }'
 10  );
1 row created.

ngarg> INSERT INTO DEPT_JSON
  2  VALUES(20,
  3   '{"DEPTNO": "20",
  4     "DNAME": "RESEARCH",
  5     "EMPLOYEES" : [{ EMPNO: 7566, "ENAME": "JONES", SAL: 2975, "JOB": "MANAGER"},
  6                    { EMPNO: 7788, "ENAME": "SCOTT", SAL: 3000, "JOB": "ANALYST"},
  7                    { EMPNO: 7902, "ENAME": "FORD", SAL: 3000, "JOB": "ANALYST"},
  8    { EMPNO: 7369, "ENAME": "SMITH", SAL: 800, "JOB": "CLERK"},
  9    { EMPNO: 7876, "ENAME": "ADAMS", SAL: 1100, "JOB": "CLERK"}
 10   ]
 11   }'
 12  );
1 row created.

ngarg> INSERT INTO DEPT_JSON
  2  VALUES(30,
  3   '{"DEPTNO": "30",
  4     "DNAME": "RESEARCH",
  5     "EMPLOYEES" : [{ EMPNO: 7698, "ENAME": "BLAKE", SAL: 2850, "JOB": "MANAGER"},
  6                    { EMPNO: 7499, "ENAME": "ALLEN", SAL: 1600, "JOB": "SALESMAN"},
  7                    { EMPNO: 7521, "ENAME": "WARD", SAL: 1250, "JOB": "SALESMAN"},
  8                    { EMPNO: 7654, "ENAME": "MARTIN", SAL: 1250, "JOB": "SALESMAN"},
  9                    { EMPNO: 7844, "ENAME": "TURNER", SAL: 1500, "JOB": "SALESMAN"},
 10                    { EMPNO: 7900, "ENAME": "JAMES", SAL: 950, "JOB": "CLERK"}
 11   ]
 12   }'
 13  );
1 row created.

ngarg> commit;
Commit complete.

As you would have noticed, I have not specified any different data type to store JSON, JSON is simply being stored in CLOB field. Now we can use following SQL to parse Nested JSON data along with JSON Array in Oracle Database by using JSON_TABLE function.

ngarg> SELECT
  2    DEPTNO, DNAME, EMPNO, ENAME, JOB, SAL
  3  FROM
  4    DEPT_JSON D,
  5    JSON_TABLE
  6    (
  7      D.JSON_VALUE, '$' COLUMNS
  8      (
  9        DEPTNO NUMBER(4) PATH '$.DEPTNO',
 10        DNAME VARCHAR2(15) PATH '$.DNAME',
 11        NESTED path '$.EMPLOYEES[*]' COLUMNS
 12        (
 13          EMPNO NUMBER(5) PATH '$.EMPNO',
 14          ENAME VARCHAR2(15) PATH '$.ENAME',
 15          JOB VARCHAR2(15) PATH '$.JOB',
 16          SAL NUMBER(7,2) PATH '$.SAL'
 17        )
 18      )
 19  ) J
 20  ORDER BY DEPTNO;

    DEPTNO DNAME                EMPNO ENAME           JOB                    SAL
---------- --------------- ---------- --------------- --------------- ----------
        10 ACCOUNTING            7839 KING            PRESIDENT             5000
        10 ACCOUNTING            7934 MILLER          CLERK                 1300
        10 ACCOUNTING            7782 CLARK           MANAGER               2450
        20 RESEARCH              7369 SMITH           CLERK                  800
        20 RESEARCH              7788 SCOTT           ANALYST               3000
        20 RESEARCH              7566 JONES           MANAGER               2975
        20 RESEARCH              7902 FORD            ANALYST               3000
        20 RESEARCH              7876 ADAMS           CLERK                 1100
        30 RESEARCH              7698 BLAKE           MANAGER               2850
        30 RESEARCH              7499 ALLEN           SALESMAN              1600
        30 RESEARCH              7521 WARD            SALESMAN              1250
        30 RESEARCH              7900 JAMES           CLERK                  950
        30 RESEARCH              7844 TURNER          SALESMAN              1500
        30 RESEARCH              7654 MARTIN          SALESMAN              1250

14 rows selected.

JSON_TABLE can be used in Oracle Database to specify the structure of JSON data, and convert JSON data into relational. As you can see, JSON_TABLE is easy to use. We simply need to define the PATH of each value in JSON and if data is nested we can use NESTED PATH, and Oracle database will automatically convert single JSON data with nested JSON into multiple relational rows. At the end of this post, I would also like to recommend to read JSON in Oracle Database with Examples to understand the how can we simply use dot (.) notation in SQL to read JSON Data in Oracle database

Related Posts
- JSON in Oracle Database with Examples
- Convert XML to Rows and Columns in Oracle
- Generate XML data using SQL in Oracle Database
- Load XML File in Oracle Table
- XML output from SQL query
- Create CSV file using PL/SQL
- Load CSV file in Oracle using PL/SQL
- SQL*Loader - Load Excel/CSV file into Oracle Database Table
- SQLFormat - CSV, JSON, XML - Format output of SQL in SQLDeveloper

51 comments:

  1. >It is very good and useful .Learned a lot of new things from your post!Good creation ,thanks for good info oracle training in chennai

    ReplyDelete
  2. Thank you for sharing such a great information with us .
    India’s No. 1 Bulk SMS Platform

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for this lovely article. You can see the difference between SQL and PLSQL also in simple words .
    Link for the same :
    https://www.complexsql.com/difference-between-sql-and-pl-sql/


    Hope you will most more comments like this.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Wow, what a nice blog. I have read it and really impressed by its writing.

    Meerahini is the best site to buy Jaipur Salwar Suits Online and Hand block Kurta sets. We have straight suit sets, anarkali suit sets, kurta and plazzo sets, and Sharara Sets. We have premium clothing collection of Rang and Utsav.a

    ReplyDelete
  7. GCL is one of India's leading financial services group with decades of experience in the financial market. lEARN More: best stock trading companies in jaipur

    ReplyDelete
  8. Infycle Technologies, the best software training institute in Chennai offers the No.1 Python Certification in Chennai for tech professionals. Apart from the Python Course, other courses such as Oracle, Java, Hadoop, Selenium, Android, and iOS Development, Big Data will also be trained with 100% hands-on training. After the completion of training, the students will be sent for placement interviews in the core MNC's. Dial 7502633633 to get more info and a free demo.

    ReplyDelete
  9. Thanks for sharing wonderful Information. In this article I learn a lot. And if you want to know m about wedding or matrimonial services visit:- Truelymarry
    One of the best Indian matrimonial site in Kanpur TruelyMarry
    We provide services:-Kanpur , Manglik , Second Marriage




    ReplyDelete
  10. Good one this is one of the very good blog. It has increased my knowledge

    Alteza is one of the best Web and App developer company. Located in India

    visit us:- http://altezatel.com/

    ReplyDelete
  11. Thanks for the marvelous posting! I actually enjoyed reading it, you will be a great author. I want to encourage continue your great writing. Thanks for sharing your nice topic.

    Telemedicine App Development

    ReplyDelete
  12. Hi, I am John Smith I am Web Developer, It is an amazing blog thanks for the sharing the blog. Frantic infotech provide the ios ui ux app development company such as an information about software development for costumer service. Frantic infotech also provide the react native mobile app development. Theve delopment of advanced web applications is Orient Software’s specialty and we will successfully fulfill all your web application development requirements, from small-sized to wider-ranged projects.

    ReplyDelete
  13. Very helpful blog! Thnak you for sharing.

    Alteza is a leading online pharmacy app development company and we address every concern of our clients by providing comprehensive online pharmacy apps for their business needs.

    ReplyDelete
  14. Wecadenceindia is a Best Digital Marketing Agency In Noida. For any kind of digital marketing service contact them.

    ReplyDelete
  15. The great website and information shared are also very appreciable. Spiderman Hoodie

    ReplyDelete
  16. You have my sincere gratitude for giving me access to this fantastic material. I have gained a lot of knowledge from your content.

    popular tractor price

    ReplyDelete
  17. Comparison Charts are best to use when you are looking for something specific, say in case you need to buy something, this chart will help you look at their advantages, disadvantages, prices, reviews, features, etc. Thus, making it easier to choose between them easily. Read more here :함평아로마
    영광아로마
    장성아로마
    신안아로마
    완주아로마
    진안아로마
    무주아로마
    장수아로마

    ReplyDelete
  18. Your writing is impressive, and your helpful advice is greatly appreciated.

    4x4 tractor price

    ReplyDelete
  19. Web designing, Web configuration, Web content turn of events, customer contact, customer side/worker side scripting, Web worker and organization security arrangement, and web based business improvement. Programming advancement is the way toward considering, indicating, planning, programming, archiving, testing, and bug fixing associated with making and looking after applications, structures, or other programming segments.Software Company in Agra Programming improvement is an interaction of composing and keeping up the source code, yet from a more extensive perspective, it incorporates all that is included between the origination of the ideal programming through to the last sign of the product, now and again in an arranged and organized cycle. Subsequently, programming improvement may incorporate exploration, new turn of events, prototyping, alteration, reuse, re-designing, support, or whatever other exercises that bring about programming items

    ReplyDelete
  20. very informative collection you have thank for sharing with us. silent readers

    ReplyDelete
  21. If you are looking for personal trainer delhi then please contact us for more details.

    ReplyDelete
  22. If you are looking for indian email service then Mailing solution is a popular bulk email service provider that offers a range of features, including email automation, landing pages, and web push notifications.

    ReplyDelete
  23. HACK ATM AND BECOME TODAY RICH You can hack and crash into it without having too much weap남원출장샵ons or weapons. How is that possible?

    ReplyDelete
  24. 82 pg สล็อต ออนไลน์ เพียงแค่คุณมีอินเทอร์เน็ตและอุปกรณ์ที่เชื่อมต่ออย่างเรียบร้อย สามารถเข้าสู่เว็บไซต์หรือแอปพลิเคชันของ pg slot ได้ทันทีสมัครสมาชิกได้อย่างรวดเร็วมาก

    ReplyDelete
  25. Amazing blog and gives a lot of idea about Oracle, there are various things that are not known oracle are cleared in this blog. We have several other blogs to read
    Healthcare software development blog
    Telemedicine app development
    EHR software development
    Healthcare software development company

    ReplyDelete
  26. Thanks for sharing. Great blog! I finally found something that helped me. Keep up the amazing work. Check our new rhiannon fish measurements coat .

    ReplyDelete