获取嵌套JSON列的值

时间:2019-02-17 17:00:12

标签: sql json oracle

我有以下内容:

WITH json AS (  
   SELECT '{"date":"2019-02-14"
        ,"data":{"AAPL":{"open":"169.71","close":"170.80","high":"171.26","low":"169.38","volume":"21175371"}
               ,"MSFT":{"open":"106.31","close":"106.90","high":"107.29","low":"105.66","volume":"21621821"}}}' doc  
   FROM dual  
)  

SELECT jt1.*
FROM 
json j,  
json_table(j.doc, '$'
       COLUMNS
       CLOSE_DATE varchar2(200) PATH '$.date'
     , NESTED  PATH '$.data[*]'  COLUMNS (
       ticker varchar2(2000) PATH '$.*',
       NESTED PATH '$.*' COLUMNS
                     (                       
                        opn   VARCHAR2(2000) PATH '$.open',
                        clse  VARCHAR2(2000) PATH '$.close',
                        hgh   VARCHAR2(2000) PATH '$.high',
                        lows  VARCHAR2(2000) PATH '$.low',
                        volu  VARCHAR2(2000) PATH '$.volume'
                     )                   
                    )
                   ) jt1;

它返回除两个股票代码以外的所有值。如何获得这些价值?我正在使用ORACLE XE 18C

1 个答案:

答案 0 :(得分:1)

使用您的数据,您可以扁平提取两个股票代号在以股票代号命名的单独列中。

WITH json AS (  
   SELECT '{"date":"2019-02-14"
        ,"data":{"AAPL":{"open":"169.71","close":"170.80","high":"171.26","low":"169.38","volume":"21175371"}
               ,"MSFT":{"open":"106.31","close":"106.90","high":"107.29","low":"105.66","volume":"21621821"}}}' doc  
   FROM dual  
)  
SELECT jt1.*
FROM 
json j,  
json_table(j.doc, '$'
       COLUMNS 
       CLOSE_DATE varchar2(200) PATH '$.date'
     , NESTED  PATH '$.data'  COLUMNS (
       NESTED PATH '$.AAPL' COLUMNS
                     (                       
                        AAPL_opn   VARCHAR2(2000) PATH '$.open',
                        AAPL_clse  VARCHAR2(2000) PATH '$.close',
                        AAPL_hgh   VARCHAR2(2000) PATH '$.high',
                        AAPL_lows  VARCHAR2(2000) PATH '$.low',
                        AAPL_volu  VARCHAR2(2000) PATH '$.volume'
                     ) ,
       NESTED PATH '$.MSFT' COLUMNS
                     (                       
                        MSFT_opn   VARCHAR2(2000) PATH '$.open',
                        MSFT_clse  VARCHAR2(2000) PATH '$.close',
                        MSFT_hgh   VARCHAR2(2000) PATH '$.high',
                        MSFT_lows  VARCHAR2(2000) PATH '$.low',
                        MSFT_volu  VARCHAR2(2000) PATH '$.volume'
                     )                     
                    )
                   ) jt1;

但这不会扩展以获取更多数据。 您可能想要的是一个包含股票代码的数组。

请注意已更改的JSON数据

WITH json AS (  
   SELECT '{"date":"2019-02-14"
        ,"data":[{"ticker":"AAPL","open":"169.71","close":"170.80","high":"171.26","low":"169.38","volume":"21175371"}
               , {"ticker":"MSFT","open":"106.31","close":"106.90","high":"107.29","low":"105.66","volume":"21621821"}]}' doc  
   FROM dual  
)  

SELECT jt1.*
FROM 
json j,  
json_table(j.doc, '$'
       COLUMNS (
       CLOSE_DATE varchar2(200) PATH '$.date'
     , NESTED  PATH '$.data[*]'  COLUMNS 
                     (  ticker VARCHAR2(2000) PATH '$.ticker',                      
                        opn   VARCHAR2(2000) PATH '$.open',
                        clse  VARCHAR2(2000) PATH '$.close',
                        hgh   VARCHAR2(2000) PATH '$.high',
                        lows  VARCHAR2(2000) PATH '$.low',
                        volu  VARCHAR2(2000) PATH '$.volume'
                     ) 
                    ) 
                   ) jt1;

结果

CLOSE_DATE           TICKER               OPN                  CLSE                 HGH                  LOWS                 VOLU               
-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
2019-02-14           AAPL                 169.71               170.80               171.26               169.38               21175371             
2019-02-14           MSFT                 106.31               106.90               107.29               105.66               21621821