0

I have a JSON column in a table & I'd love to turn it into a normal table using SQL;

JSON sample;

{  
   "request":{  
      "createPartyAssetCriteria":null,
      "createPartyExpenseCriteria":[  
         {  
            "correlationID":"123456789",
            "partyID":"123456789",
            "related":false,
            "relatedPartyID":"123456789",
            "expenseTypeCode":"RENT",
            "expenseDescription":"Monthly rent",
            "expenseAmount":123,
            "currencyCode":"USD",
            "institutionName":null
         }
      ],
      "createPartyLiabilityCriteria":null,
      "createFinancialInstitutionProductCriteria":null
   },
   "response":{  
      "partyAssetContainers":null,
      "partyLiabilityContainers":null,
      "financialInstitutionProductContainers":null,
      "partyExpenseContainers":[  
         {  
            "partyExpenseID":"123456789",
            "processStatusCode":"S",
            "errorCode":null,
            "errorMessage":null,
            "correlationID":"createPartyExpense1"
         }
      ],
      "error":false,
      "errorMessage":null
   },
   "systemName":"Financial Position"
}

Table Sample - what I'd like to get;

Column1 Column2                     Column3         Column4     Value  
request     createPartyAssetCriteria                                null  
request     createPartyExpenseCriteria  correlationID               1277574622  
request     createPartyExpenseCriteria  partyID                     123456789  
request     createPartyExpenseCriteria  related                     false  
request     createPartyExpenseCriteria  relatedPartyID              123456789  
request     createPartyExpenseCriteria  expenseTypeCode             RENT  
etc...

I've been playing around with this but I'm nowhere near a solution yet - have not even addressed the "vertical" nature of the requirement;

select substr(json_string,instr(json_string,'"',1,1 )+1,instr(json_string,'"',1,2 )-(instr(json_string,'"',1,1 )+1)) level1,
       substr(json_string,instr(json_string,'"',1,3 )+1,instr(json_string,'"',1,4 )-(instr(json_string,'"',1,3 )+1)) level2,
       substr(json_string,instr(json_string,'"',1,5 )+1,instr(json_string,'"',1,6 )-(instr(json_string,'"',1,5 )+1)) level3,
       substr(json_string,instr(json_string,'"',1,7 )+1,instr(json_string,'"',1,8 )-(instr(json_string,'"',1,7 )+1)) level4,
       substr(json_string,instr(json_string,'"',1,9 )+1,instr(json_string,'"',1,10)-(instr(json_string,'"',1,9 )+1)) level5,
       substr(json_string,instr(json_string,'"',1,11)+1,instr(json_string,'"',1,12)-(instr(json_string,'"',1,11)+1)) level6,
       substr(json_string,instr(json_string,'"',1,13)+1,instr(json_string,'"',1,14)-(instr(json_string,'"',1,13)+1)) level7,
       substr(json_string,instr(json_string,'"',1,15)+1,instr(json_string,'"',1,16)-(instr(json_string,'"',1,15)+1)) level8,
       substr(json_string,instr(json_string,'"',1,17)+1,instr(json_string,'"',1,18)-(instr(json_string,'"',1,17)+1)) level9,
       json_string
from event where event_type_cd LIKE '%CRTE_EXT_FINNCL_DTLS%';
COLUMN1 COLUMN2 COLUMN3             COLUMN4 COLUMN5                     COLUMN6          
request     create  PartyAssetCriteria  null    createPartyExpenseCriteria  correlationID.. 
request     create  PartyAssetCriteria  null    createPartyExpenseCriteria  correlationID.. 
request     create  PartyAssetCriteria  null    createPartyExpenseCriteria  correlationID..

Has anyone come across this?

It's a significant effort & I need to make a call if I'm willing to commit to doing this in SQL.

I have a strong preference to doing this in SQL. If there is a much easier way of doing it in Java then let me know. It wouldn't help me on my task whatsoever but might help the project. My SQL is strong but Java is not my bag - I'd have to get someone else to do it then too.

Paul
  • 47
  • 1
  • 8
  • What _exact_ Oracle version are you on? JSON support was significantly extended in 12.1.0.2. – Ben Nov 04 '16 at 14:12
  • This should give you a good start on how to convert JSON into an Oracle Table: http://stackoverflow.com/questions/27122612/parse-json-into-oracle-table-using-pl-sql – Sean H Nov 04 '16 at 14:16
  • @Ben 12.1.0.2.0 The JSON data is in a CLOB if that matters – Paul Nov 04 '16 at 15:05

0 Answers0