Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to create a clean JSON file using Oracle SQL?

I am using MS Report Builder, whilst being on Oracle 19c, to convert an SQL query to JSON.

Here is my SQL code:

SELECT json_object(
'id'        VALUE   REFVAL,
'recdate'   VALUE   DATEAPRECV,
'apptype'   VALUE   APPTYP,
'status'    VALUE   STAT,
'person'    VALUE   NAME
)

FROM APPLICATIONS

WHERE APPTYP = 'S10'

And the outcome:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Report
{"id" : "1", "recdate" : "01-01-01", "apptype" : "S10", "status" : "COMP", "person" : "John"}
{"id" : "2", "recdate" : "02-02-02", "apptype" : "S10", "status" : "REG", "person" : "Mary"}

It produces this tabular format which is not ideal.

Ideally, it would be like this.

{"id" : "1", "recdate" : "01-01-01", "apptype" : "S10", "status" : "COMP", "person" : "John"},{"id" : "2", "recdate" : "02-02-02", "apptype" : "S10", "status" : "REG", "person" : "Mary"}

Or

[
   {
    "id" : "1",
    "recdate" : "01-01-01",
    "apptype" : "S10",
    "status" : "COMP",
    "person" : "John"
   },
   {
    "id" : "2",
    "recdate" : "02-02-02",
    "apptype" : "S10",
    "status" : "REG",
    "person" : "Mary"}
]

Is there any way to reproduce this?

>Solution :

You can use JSON_ARRAYAGG() to aggregate all your JSON objects into one array:

select json_arrayagg(
    json_object(
        'id'        value   refval,
        'recdate'   value   dateaprecv,
        'apptype'   value   apptyp,
        'status'    value   stat,
        'person'    value   name
        )
    format json order by refval
) as agg
from applications
where apptyp = 'INVOICE'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading