Oracle DB version 12.2
I have the following data model with two tables:
SQL> desc TABLE_A
Name Null? Type
----------------------------------------- -------- ----------------------------
FILENAME VARCHAR2(100 CHAR)
REPORT_DATE VARCHAR2(12 CHAR)
DOMAINCODE VARCHAR2(10 CHAR)
LEGALENTITYCODE VARCHAR2(10 CHAR)
SQL> desc TABLE_B
Name Null? Type
----------------------------------------- -------- ----------------------------
FILENAME VARCHAR2(100 CHAR)
NATIVEKEY VARCHAR2(50 CHAR)
MASTERKEY VARCHAR2(50 CHAR)
ENDDATE VARCHAR2(12 CHAR)
SQL>
So, I got this basic join query to get the results I want
SELECT
a.REPORT_DATE,
a.DOMAINCODE,
a.LEGALENTITYCODE,
b.nativekey,
b.masterkey,
b.enddate
FROM TABLE_A a
INNER JOIN
TABLE_B b
ON (NVL (a.filename, 'XX') = NVL (b.filename, 'XX'))
where a.domaincode = '00001';
Which gives me :
My requirement is to print this data in the form of a JSON document like this:
{
"Date":"2020-03-31"
,"DomainCode":"00001"
,"LegalEntityCode":"00055"
,"Keys":[
{
"NativeKey":"129582692"
,"MasterKey":"329323111430011996"
,"EndDate":"9999-12-31"
}
,{
"NativeKey":"14735034"
,"MasterKey":"329390935000331576"
,"EndDate":"9999-12-31"
}
]
}
As you can see above, the hierarchy of the data appears only once in the document, at the beginning. I've been playing around with SQL/JSON functions on 12.2 , but I always got the hierarchy on each record. I found a workaround using APEX_JSON, which gives me the format required, but its performance is not very good, and I am generating a document with 10 million rows. I know I can generate this using standard SQL and escaping/printing each special character, but I don't want to do that.
Is there any way to generate this in a standard way with JSON funcions like JSON_ARRAY and/or JSON_OBJECT ?
Thank you all!
