Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Tomas.

Asked: June 14, 2018 - 8:21 pm UTC

Last updated: August 22, 2019 - 10:21 am UTC

Version: 11g

Viewed 10K+ times! This question is

You Asked

Hello!

Just a question. Is it possible to write a query that returns a JSON code?
If yes, could you give me a brief example?
Thanks!

and Connor said...

In 12.2 and above,yes, we have native functions, eg

SQL> select
  2    json_array(department_id, department_name) depts
  3  from hr.departments
  4  where department_id > 200;

DEPTS
--------------------------------------------------------
[210,"IT Support"]
[220,"NOC"]
[230,"IT Helpdesk"]
[240,"Government Sales"]
[250,"Retail Sales"]
[260,"Recruiting"]
[270,"Payroll"]

7 rows selected.


SQL> select
  2    json_object(
  3      'DeptID' is department_id,
  4      'Name'   is department_name) depts
  5  from hr.departments
  6  where department_id > 200;

DEPTS
------------------------------------------------
{"DeptID":210,"Name":"IT Support"}
{"DeptID":220,"Name":"NOC"}
{"DeptID":230,"Name":"IT Helpdesk"}
{"DeptID":240,"Name":"Government Sales"}
{"DeptID":250,"Name":"Retail Sales"}
{"DeptID":260,"Name":"Recruiting"}
{"DeptID":270,"Name":"Payroll"}

7 rows selected.


SQL>  select json_object(
  2               'department' value d.department_name,
  3               'employees' value json_arrayagg(
  4                 json_object(
  5                   'name' value first_name||','||last_name,
  6                   'job' value job_title
  7             )) returning varchar2) j
  8      from   hr.departments d, hr.employees e, hr.jobs j
  9      where  d.department_id = e.department_id
 10     and    e.job_id = j.job_id
 11     and    d.department_id = 100
 12     group  by d.department_name;

J
----------------------------------------------------------------------------------------------------
{"department":"Finance","employees":[{"name":"Nancy,Greenberg","job":"Finance Manager"},{"name":"Lui
s,Popp","job":"Accountant"},{"name":"Jose Manuel,Urman","job":"Accountant"},{"name":"Ismael,Sciarra"
,"job":"Accountant"},{"name":"John,Chen","job":"Accountant"},{"name":"Daniel,Faviet","job":"Accounta
nt"}]}
     


Before that, you'd need to craft them by hand. Here's an example by a friend Lucas as AMIS

https://technology.amis.nl/2011/06/14/creating-json-document-straight-from-sql-query-using-listagg-and-with-clause/

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank you so much!

Tomas Juarez, June 15, 2018 - 3:56 pm UTC

That's great, thanks a lot.
It's not possible in 11g, right?
Connor McDonald
June 16, 2018 - 1:15 am UTC

Correct - see the last couple of lines of my anwswer

Tomas Juarez, June 18, 2018 - 1:50 pm UTC

Oh, you're right.
Thank you so much, that info is exactly what I needed.
Connor McDonald
June 19, 2018 - 3:21 am UTC

glad we could help

using SQL CI

Rajeshwaran, Jeyabal, June 21, 2018 - 3:04 pm UTC

If you are on Oracle 11g - then you can think of using SQLFORMAT option in SQLCL utility.

something like this.

demo@ORA11G> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for 64-bit Windows: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production


demo@ORA11G> set sqlformat json
demo@ORA11G> select * from scott.dept;
{"results":[{"columns":[{"name":"DEPTNO","type":"NUMBER"},{"name":"DNAME","type":"VARCHAR2"},{"name":"LOC","type":"VARCHAR2"}],"items":
[
{"deptno":10,"dname":"ACCOUNTING","loc":"NEW YORK"},{"deptno":20,"dname":"RESEARCH","loc":"DALLAS"},{"deptno":30,"dname":"SALES","loc":"CHICAGO"},{"deptno":40,"dname":"OPERATIONS","loc":"BOST
ON"}]}]}

demo@ORA11G> set sqlformat
SQL Format Cleared
demo@ORA11G>


Please check if that helps in your case.

JSON in 11g

B C, August 21, 2019 - 7:44 pm UTC

I would like to generate JSON output from an SQL Statement in PL/SQL on 11g, unfortunately SQLCL is not an option.

Basically, I want to select data from a table and save the output as a CLOB in another table. The data needs to be stored in JSON Format.
Chris Saxon
August 22, 2019 - 10:21 am UTC

If you're not a fan of constructing it yourself, you have a few options:

- Use a third-party library, such as PL/JSON https://github.com/pljson/pljson
- Use APEX_JSON https://jsao.io/2015/07/relational-to-json-with-apex_json/
- Use SQLcl/SQL Dev to spool the JSON to a file; then load this file into the other table

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.