使用Oracle SQL提取JSON数据

jgwigjjp  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(267)

Oracle DB版本19.2我们第一次尝试使用JSON数据集,没有使用它的经验。我们正在阅读与JSON相关的Oracle文档,但发现很难理解。我们需要帮助来查询JSON文件中的数据,这些JSON文件中的元素是JSON对象中嵌套数组的一部分。任何帮助真的很感激。谢谢.

这里有一个JSON文件供参考

{

"accountDetails": {

"accountName": "My Account",

"custodianAccount": "My AccountNumber",

"startDate": "220150615",

"endDate": "20230703",

"marketValue": {

"currencyCode": "CAD",

"amount": 737995.96

},

"performance": [

{

"periodLabel": "Inception-to-Date",

"twrrNof": 0.03523584423021675,

"twrrGof": 0.04128811745224015,

"periodStart": "20150616",

"periodEnd": "20230703"

},

{

"periodLabel": "Quarter-to-Date",

"twrrNof": 7.559920027098155,

"twrrGof": 7.559924692275288,

"periodStart": "20230701",

"periodEnd": "20230703"

}

],

},

"sleeves": {

"sleeveDetails": [

{

"sleeveName": "Sleeve1",

"inceptionDate": "20210318",

"marketValue": {

"currencyCode": "CAD",

"amount": 154312.67

},

"performance": [

{

"periodLabel": "Inception-to-Date",

"twrrNof": 1.59171883177918061,

"twrrGof": 0.09171883177918061,

"periodStart": "20210319",

"periodEnd": "20230703"

},

{

"periodLabel": "Quarter-to-Date",

"twrrNof": 1.503752211169898434,

"twrrGof": 5.53752211169898434,

"periodStart": "20230701",

"periodEnd": "20230703"

}

],

},

{

"sleeveName": "Sleeve2",

"inceptionDate": "20210825",

"marketValue": {

"currencyCode": "CAD",

"amount": 262423.86

},

"performance": [

{

"periodLabel": "Inception-to-Date",

"twrrNof": 2.53041295906685626,

"twrrGof": 0.03041295906685626,

"periodStart": "20210826",

"periodEnd": "20230703"

},

{

"periodLabel": "Quarter-to-Date",

"twrrNof": 2.4019374838543011474,

"twrrGof": 0.0019374838543011474,

"periodStart": "20230701",

"periodEnd": "20230703"

}

],

},

{

"sleeveName": "Sleeve3",

"inceptionDate": "20151008",

"marketValue": {

"currencyCode": "CAD",

"amount": 321259.43

},

"performance": [

{

"periodLabel": "Inception-to-Date",

"twrrNof": 3.5362634897972308,

"twrrGof": 5.562634897972308,

"periodStart": "20151009",

"periodEnd": "20230703"

},

{

"periodLabel": "Quarter-to-Date",

"twrrNof": 3.511802131625067158,

"twrrGof": 5.5011802131625067158,

"periodStart": "20230701",

"periodEnd": "20230703"

}

],

},

]

}

}

输出到JSON列Map:

  • accountName = accountDetails.accountName
  • custodianAccount = accountDetails.custodianAccount
  • accountMarketValue = accountDetails.marketValue.amount
  • account_twrrNof_Inception-to-Date = accountDetails.performance.twrrNof(其中periodLabel ='Inception-to-Date')
  • account_twrrNof_Quarter-to-Date = accountDetails.performance.twrrNof(其中periodLabel ='Quarter-to-Date')
  • sleeveName = sleeves.sleeveDetails.sleeveName
  • sleeveMarketValue = sleeve.sleeveDetails.marketValue.amount
  • sleeve_twrrNof_Inception-to-Date = sleeves.sleeveDetails.performance.twrrNof(其中periodLabel ='Inception-to-Date')
  • sleeve_twrrNof_Quarter-to-Date = sleeves.sleeveDetails.performance.twrrNof(其中periodLabel ='Quarter-to-Date')
    注,
  • for columns account_twrrNof_为了简单起见,我在这里只保留了2列,但是account有8个不同的性能数字,所以当我们读取twrrNof的值时,我们还需要知道它是哪个时间段的,例如将它与periodLabel ='Inception-to-Date'Map意味着这个twrNof值是account_twrrNof_Inception-to-Date。JSON文件中只需要1个帐户。
  • for columns sleeve_twrrNof_ Same like account level performance,为了简单起见,我在这里只保留了2列,但是每个sleeve有8个不同的性能数字,所以当我们读取twrNof的值时,我们还需要知道它是哪个时间段的,例如将其Map为periodLabel ='Inception-to-Date'意味着这个twrNof值是sleeve_twrNof_Inception-to-Date。此外,账户在文件中可能有1个或多个sleeve,因此在JSON文件中可能有至少1个或多个sleeve和相关性能。

我们在查询JSON方面非常新,我们使用JSON_VALUE获取帐户的基本细节,并尝试使用JSON_TABLE,但无法按预期获取嵌套值。

Expected Query output这里是我们从上面的JSON enter image description here读取数据时希望查询输出的样子

uubf1zoe

uubf1zoe1#

如果你修复了你的JSON,使它有效,那么你可以使用:用途:

SELECT j.*
FROM   table_name t
       CROSS APPLY JSON_TABLE(
         t.value,
         '$'
         COLUMNS
           accountName
             VARCHAR2(200) PATH '$.accountDetails.accountName',
           custodianAccount
              VARCHAR2(200) PATH '$.accountDetails.custodianAccount',
           accountMarketValue
              NUMBER        PATH '$.accountDetails.marketValue.amount',
           account_twrrNof_Inception_to_Date
              VARCHAR2(200) PATH '$.accountDetails.performance[*]?(@.periodLabel=="Inception-to-Date").twrrNof',
           account_twrrNof_Quarter_to_Date
              VARCHAR2(200) PATH '$.accountDetails.performance[*]?(@.periodLabel=="Quarter-to-Date").twrrNof',
           NESTED PATH '$.sleeves.sleeveDetails[*]'
           COLUMNS (
             sleeveName 
               VARCHAR2(200) PATH '$.sleeveName',
             sleeveMarketValue
               NUMBER PATH '$.marketValue.amount',
             sleeve_twrrNof_Inception_to_Date
               VARCHAR2(200) PATH '$.performance[*]?(@.periodLabel=="Inception-to-Date").twrrNof',
             sleeve_twrrNof_Quarter_to_Date
               VARCHAR2(200) PATH '$.performance[*]?(@.periodLabel=="Quarter-to-Date").twrrNof'
           )
       ) j;

对于(有效)样本数据:

CREATE TABLE table_name (value CLOB CHECK (value IS JSON));

INSERT INTO table_name (value)
VALUES (
'{"accountDetails":{"accountName":"My Account","custodianAccount":"My AccountNumber","startDate":"220150615","endDate":"20230703","marketValue":{"currencyCode":"CAD","amount":737995.96},"performance":[{"periodLabel":"Inception-to-Date","twrrNof":0.03523584423021675,"twrrGof":0.04128811745224015,"periodStart":"20150616","periodEnd":"20230703"},{"periodLabel":"Quarter-to-Date","twrrNof":7.559920027098155,"twrrGof":7.559924692275288,"periodStart":"20230701","periodEnd":"20230703"}]},"sleeves":{"sleeveDetails":[{"sleeveName":"Sleeve1","inceptionDate":"20210318","marketValue":{"currencyCode":"CAD","amount":154312.67},"performance":[{"periodLabel":"Inception-to-Date","twrrNof":1.5917188317791806,"twrrGof":0.09171883177918061,"periodStart":"20210319","periodEnd":"20230703"},{"periodLabel":"Quarter-to-Date","twrrNof":1.5037522111698984,"twrrGof":5.537522111698984,"periodStart":"20230701","periodEnd":"20230703"}]},{"sleeveName":"Sleeve2","inceptionDate":"20210825","marketValue":{"currencyCode":"CAD","amount":262423.86},"performance":[{"periodLabel":"Inception-to-Date","twrrNof":2.5304129590668563,"twrrGof":0.03041295906685626,"periodStart":"20210826","periodEnd":"20230703"},{"periodLabel":"Quarter-to-Date","twrrNof":2.4019374838543013,"twrrGof":0.0019374838543011474,"periodStart":"20230701","periodEnd":"20230703"}]},{"sleeveName":"Sleeve3","inceptionDate":"20151008","marketValue":{"currencyCode":"CAD","amount":321259.43},"performance":[{"periodLabel":"Inception-to-Date","twrrNof":3.5362634897972307,"twrrGof":5.562634897972308,"periodStart":"20151009","periodEnd":"20230703"},{"periodLabel":"Quarter-to-Date","twrrNof":3.511802131625067,"twrrGof":5.501180213162507,"periodStart":"20230701","periodEnd":"20230703"}]}]}}');

输出:
| accountName| CUSTODIANACHIO|帐户市场价值|接受开始日期|截至本季度末,|SLEEVENAME| SLEEVEMARKETVALUE|套管_TWRRNOF_开始日期|套筒_TWRRNOF_QUARTER_TO_DATE|
| --|--|--|--|--|--|--|--|--|
| 我的帐户|我的账号|737995.96|电话:03523584423021675| 7.559920027098155|套筒1| 154312.67| 1.5917188317791806| 1.5037522111698984|
| 我的帐户|我的账号|737995.96|电话:03523584423021675| 7.559920027098155|套筒2| 262423.86| 2.5304129590668563| 2.4019374838543013|
| 我的帐户|我的账号|737995.96|电话:03523584423021675| 7.559920027098155|套筒3| 321259.43| 3.5362634897972307| 3.511802131625067|
fiddle

相关问题