【问题标题】:Is it possible to use wildcards and ranges with JSON_VALUE?是否可以在 JSON_VALUE 中使用通配符和范围?
【发布时间】:2015-02-16 23:07:11
【问题描述】:

我目前正在研究一个使用 Oracle 12c 的 JSON 功能的示例,我正在努力理解如何将通配符和范围与 JSON_VALUE 一起使用。

我使用的example 来自 Oracle 杂志 2015 年 1 月/2 月号,这是我坚持使用的 SQL。

select json_value(t.trans_msg, '$.CheckDetails[0].CheckNumber'), -- works returns 101
       json_value(t.trans_msg, '$.CheckDetails[*].CheckNumber'), -- null returned      
       json_value(t.trans_msg, '$.CheckDetails[0,1].CheckNumber') -- null returned      
 from transactions t
where t.id = 1 

文章提到

...显示所有 值,Mark 使用 CheckDetails[*]。 (他可以使用特定的数字来 拉取特定项目——例如,CheckDetails[0,2] 拉取第一个 第三项...

所以我希望查询的第二行和第三行返回所有 CheckNumber 值(101 和 102)。

我使用的是 Oracle 数据库 12.1.0.2.0,这里是表格和插入脚本以备不时之需:

create table transactions(
   id number not null primary key,
   trans_msg clob,
   constraint check_json check (trans_msg is json)
)
/

insert into transactions
(id, 
 trans_msg
 )
 VALUES
 (1,
 '{
       "TransId"       :    1,
       "TransDate"     :    "01-JAN-2015",
       "TransTime"     :    "11:05:00",
       "TransType"     :    "Deposit",
       "AccountNumber" :    123,
       "AccountName"   :    "Smith, John",
       "TransAmount"   :    100.00,
       "Location"      :    "ATM",
       "CashierId"     :    null,
       "ATMDetails"    : {
           "ATMId"       : 301,
           "ATMLocation" : "123 Some St, Danbury CT 06810"
               },
       "WebDetails"    : {
           "URL"    : null
               },
       "Source"    :    "Check",
       "CheckDetails"  : [
                   {
                       "CheckNumber"    : 101,
                       "CheckAmount"    : 50.00,
                       "AcmeBankFlag"    : true,
                       "Endorsed"    : true
                   },
                   {
                       "CheckNumber"    : 102,
                       "CheckAmount"    : 50.00,
                       "AcmeBankFlag"    : false,
                       "Endorsed"    : true
                   }
               ]
   }'
 )
 /

我知道我一定遗漏了一些明显的东西!

【问题讨论】:

    标签: json oracle oracle12c


    【解决方案1】:

    JSON_VALUE 旨在返回标量。要返回 JSON 对象或关系数据,您必须使用 JSON_QUERYJSON_TABLE

    select json_query(t.trans_msg, '$.CheckDetails[0].CheckNumber' with wrapper)   a,
           json_query(t.trans_msg, '$.CheckDetails[*].CheckNumber' with wrapper)   b,
           json_query(t.trans_msg, '$.CheckDetails[0,1].CheckNumber' with wrapper) c
    from transactions t
    where t.id = 1;
    
    A       B           C
    -----   ---------   ---------
    [101]   [101,102]   [101,102]
    
    
    select id, details
    from transactions
    cross join json_table
    (
        trans_msg, '$.CheckDetails[*]'
        columns
        (
            details path '$.CheckNumber'
        )
    );
    
    ID   DETAILS
    --   -------
    1    101
    1    102
    

    【讨论】:

    • 谢谢乔恩 - 在阅读了您的回答并返回文档后,我可以看到我哪里出错了。为您的用户页面上的 cmets +1。
    • 有 MSSQL 的等价物吗?
    • @SJ1 我不确定,但您可能应该在单独的线程中问这个问题。数据库问题和答案很少适用于一种以上的产品。
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多