【问题标题】:Convert JSON in a format can be read by JSON_VALUE in oracle将 JSON 转换成 oracle 中 JSON_VALUE 可以读取的格式
【发布时间】:2021-04-01 18:21:14
【问题描述】:

我有 JSON 格式的数据 -

{"dims":{"ABC":{"dim":"ABC","bins":{"1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},"345":{"bin":"345","hits":"2","first_date":"03.11.15"},"6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},"6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}}}}}

如何将其转换为格式-

{"dims":{"BINDUNGS_ID_CARDS":{"dim":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15"},{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date":"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}}

我想通过 JSON_VALUE(value, $.*.*.bins[0]) 使用 JSON_VALUE 函数从 oracle 读取它。

我不知道如何通过 oracle 实现它。

【问题讨论】:

    标签: json oracle oracle12c


    【解决方案1】:

    您可以使用以下方法重新格式化它:

    SELECT j.*
    FROM   table_name t
           CROSS APPLY (
             SELECT JSON_OBJECT(
                       'dims' VALUE JSON_OBJECT(
                         'BINDUNGS_ID_CARDS' VALUE JSON_OBJECT(
                           'dims' VALUE 'BINDUNGS_ID_CARDS',
                           'bins' VALUE JSON_ARRAYAGG(
                             data FORMAT JSON ORDER BY ROWNUM
                           )
                         )
                       )
                    ) As value
             FROM   JSON_TABLE(
                      t.value,
                      '$.*.*.bins.*'
                      COLUMNS (
                        data CLOB FORMAT JSON PATH '$'
                      )
                    )
           ) j
    

    其中,对于样本数据:

    CREATE TABLE table_name ( value CLOB CHECK ( value IS JSON ) );
    
    INSERT INTO table_name ( value )
    VALUES (
    '{
      "dims":{
        "ABC":{
          "dim":"ABC",
          "bins":{
            "1290":{"bin":"1290","hits":"1","first_date":"03.11.15"},
            "345":{"bin":"345","hits":"2","first_date":"03.11.15"},
            "6603":{"bin":"6603","hits":"1","first_date":"23.12.15"},
            "6609":{"bin":"6609","hits":"1","first_date":"13.12.15"}
          }
        }
      }
    }' );
    

    输出:

    |价值 | | :------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------ | | {"dims":{"BINDUNGS_ID_CARDS":{"dims":"BINDUNGS_ID_CARDS","bins":[{"bin":"1290","hits":"1","first_date":"03.11.15" },{"bin":"345","hits":"2","first_date":"03.11.15"},{"bin":"6603","hits":"1","first_date" :"23.12.15"},{"bin":"6609","hits":"1","first_date":"13.12.15"}]}}} |

    但是,这感觉像是一个 XY 问题。

    我想通过 JSON_VALUE(value, $.*.*.bins[0]) 使用 JSON_VALUE 函数从 oracle 读取它。

    如果你想要对象的第一个值,那么你不需要重新格式化 JSON 并且可以直接使用:

    SELECT bin,
           hits,
           TO_DATE( first_date, 'DD.MM.RR' ) AS first_date
    FROM   table_name t
           CROSS APPLY JSON_TABLE(
             t.value,
             '$.*.*.bins.*'
             COLUMNS (
               rn         FOR ORDINALITY,
               bin        VARCHAR2(20) PATH '$.bin',
               hits       VARCHAR2(20) PATH '$.hits',
               first_date VARCHAR2(8) PATH '$.first_date'
             )
           ) j
    WHERE  rn = 1
    

    哪些输出:

    斌 |热门 |第一次约会 :--- | :--- | :----------------- 1290 | 1 | 2015-11-03 00:00:00

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2021-01-14
      • 2019-08-05
      • 2021-02-14
      • 2020-04-12
      相关资源
      最近更新 更多