使用SQL将JSON字符串转换为表

时间:2016-11-04 14:08:52

标签: sql json oracle

我在表格中有一个JSON列&我想用SQL将它变成普通的表格;

JSON示例;

{  
   "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"
}

表格样本 - 我想得到什么;

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...

我一直在玩这个,但我还没有解决方案 - 甚至没有解决要求的“垂直”性质;

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..

有没有人遇到过这个?

这是一项重大的努力。如果我愿意承诺在SQL中执行此操作,我需要进行调用。

我非常喜欢在SQL中执行此操作。如果在Java中有一个很多更简单的方法,请告诉我。它对我的任务无济于事,但可能对项目有所帮助。我的SQL很强大,但Java不是我的包 - 我也必须让别人去做。

0 个答案:

没有答案