【问题标题】:query json array having only strings oracle查询只有字符串的json数组oracle
【发布时间】:2021-01-24 21:53:58
【问题描述】:

下面是存储在名为“Sample”的表中的 json,列名为“argument”。我想获取所有在指定参数中具有特定值的记录。我可以查询参数名称,但不能查询特定值,因为它是一个字符串数组。 (请找到我的钥匙里面有 .)

{
 "arguments":{
    "app.argument1.appId":["123", "456"],
    "app.argument2.testId":["546", "567"]
 }
}

这给了我所有具有特定论点的记录。

 select * from sample where json_exists(argument, '$.arguments."app.argument1.appId"');

但我需要匹配参数值。我在下面尝试过,但出现 JSON 表达式错误。

select * from sample where json_exists(argument, '$.arguments."app.argument1.appId[*]"?(@ == "123"));

请帮忙。提前致谢。

【问题讨论】:

    标签: json oracle oracle12c exists


    【解决方案1】:

    你的引号放错地方了;您希望在数组的方括号之前而不是之后使用双引号:

    select *
    from   sample
    where  json_exists(
             argument,
             '$.arguments."app.argument1.appId"[*]?(@ == "123")'
           );
    

    其中,对于样本数据:

    CREATE TABLE sample ( argument CLOB CHECK ( argument IS JSON ) );
    
    INSERT INTO sample ( argument ) VALUES ( '{
     "arguments":{
        "app.argument1.appId":["123", "456"],
        "app.argument2.testId":["546", "567"]
     }
    }');
    

    输出:

    |论据 | | :------------------------------------------------ -------------------------------------------------- -------------------- | | {
    "arguments":{
    "app.argument1.appId":["123", "456"],
    "app.argument2.testId":["546", "567"]
    }
    } |

    db小提琴here


    您知道在 12.1 中执行此操作的方法吗?

    您还可以将EXISTS 与相关的JSON_TABLE (which is available from Oracle 12c Release 1 (12.1.0.2)) 一起使用。:

    select *
    from   sample
    where  EXISTS (
      SELECT 1
      FROM   JSON_TABLE(
               argument,
               '$.arguments."app.argument1.appId"[*]'
               COLUMNS (
                 value VARCHAR2(100) PATH '$'
               )
             )
      WHERE  value = '123'
    );
    

    db小提琴here

    【讨论】:

    • 我已经尝试过这个查询。我刚刚发现 12.2 支持这种语法,并且我正在运行 Oracle 12.1。您知道在 12.1 中执行此操作的方法吗?
    • @rishi 已更新。如果您使用的是 12.1.0.2,那么您可以使用 JSON_TABLE
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2011-03-11
    • 2023-04-10
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多