Export Data into JSON File in Oracle 11g Using PL/SQL

In this article, I am giving an example to export data into JSON file in Oracle 11g using PL/SQL.

Follow These Steps to Export Data into JSON File in Oracle 11g

  1. First, download the Alexandria PL/SQL utility package from GITHUB using the following link: Download alexandria_plsql_utility package.
  2. After downloading the zip file, extract it and find the JSON_UTIL_PKG in the "alexandria-plsql-utils-master\alexandria-plsql-utils-master\ora" directory.
  3. Install JSON_UTIL_PKG package specification and body into your schema
  4. After that download the "WRITE_CLOB_TO_FILE" PL/SQL procedure from the following link: Download Write_Clob_To_File.
  5. Install this procedure also into your schema.

Now you can generate the JSON file from Oracle table using PL/SQL, as shown in below example.

Oracle 11g SQL to JSON Example

In the following example, we will get the data into CLOB variable using JSON_UTIL_PKG.SQL_TO_JSON function and then we will write that CLOB has JSON data into a FILE using WRITE_CLOB_TO_FILE procedure. We will pass three parameters to WRITE_CLOB_TO_FILE procedure, and they are as follows: a file name, directory object name, and the CLOB variable.

SELECT json_util_pkg.sql_to_json('select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO from emp2')

WRITE_CLOB_TO_FILE ('emp.json', 'JSON_DIR', c);

Now you can check the location of directory object JSON_DIR, and you will find the JSON file as shown in below example output:


See also:

16 thoughts on “Export Data into JSON File in Oracle 11g Using PL/SQL”

  1. Good Day!

    This method work good. Ok))

    But we need result JSON file without top section {"ROWSET":......}, only JSON result


    How can we reach this result?

    Thank you for future answer))

  2. Any way I can add Root Node ?

    Current Output : 

    Expected Output : 


    • In step 3, you will run the package script in your schema which you have downloaded in step 1.

      In step 5, you will run the procedure script in your schema which you have downloaded in step 4.

  3. I have an error when I try to compile the PGK, I am using toad and it asks for values of variables, this is for "&", any Ide how to fix this?

  4. This script doesn't appear to display square brackets for array of data.For E.g. for each deptartment, employee records are to be displayed in an array format. But those employee records for particular department should be in an array enclosed in "[" "]" brackets as json uses these square brackets to denote array. Could you clarify on this ?


Leave a Comment