【问题标题】:Search a JSON array in Oracle在 Oracle 中搜索 JSON 数组
【发布时间】:2015-07-06 02:00:33
【问题描述】:

我正在尝试使用 Oracle 12.1.0.2 中引入的新 JSON 功能

但是,我似乎找不到在 JSON 文档内的数组中查找特定值的方法。

考虑下表和数据:

create table orders
(
   id      integer not null primary key,
   details clob not null check (details is json (strict))
);

insert into orders (id, details) values 
(1, '{"products": [{ "product": 1, "quantity": 5}, {"product": 2, "quantity": 1}], "delivery_address": "My hometown"}');

insert into orders (id, details) values 
(2, '{"products": [{ "product": 42, "quantity": 1}, {"product": 10, "quantity": 2}], "comment": "Your website is too slow"}');

insert into orders (id, details) values 
(3, '{"products": [{ "product": 543, "quantity": 1}], "discount": "15"}');

insert into orders (id, details) values 
(4, '{"products": [{ "product": 738, "quantity": 12}], "discount": "32"}');

现在我正在尝试编写一个返回所有订单的 SQL 查询,其中订购了产品 #2。

我不能使用json_exists,因为它不允许数组表达式(而且我也不知道如何指定值)。

json_value 只返回一个值,所以我不能“迭代”数组值。

我试过了:

select *
from orders o
where json_value(details, '$.products[*].product') = '2';

但这并没有返回任何东西。

我也试过json_table,但这似乎也只取数组中的第一个元素:

select *
from orders o, 
     json_table(o.details, '$' columns (product_id integer path '$.products[*].product')) t
where t.product_id = 2;

但这并没有显示任何东西。显然“array_step”中的“星形扩展”不会扩展json_table中的值

所以我的问题是:

我如何(根据上面的示例数据)检索所有订购了编号为 2 的产品的订单?

我实际上是在寻找与此 Postgres 查询等效的内容:

select *
from orders
where details @> '{"products": [{"product": 2}] }';

【问题讨论】:

  • 我们在运行 JSON_TABLE 函数时遇到了多线程错误。基本上,当同时运行两个 JSON_TABLE 时,其中一个会话断开了连接。确保在使用 JSON 之前已对其进行了修补。
  • @PeterHenell:感谢您的提示。目前我只是在探索可能性,但我会记住这一点。

标签: sql arrays json oracle oracle12c


【解决方案1】:

我现在没有任何可用的 oracle 安装,但我相信 json_table 中的第一个字符串应该是我们要从中生成行的数组的路径。 那么在COLUMNS里面,路径应该是相对于数组的,而不是根的。

试试这个:

select *
from orders o, 
     json_table(o.details, '$.products[*]' 
         columns (
              product_id integer path '$.product'
         )
     ) t
where t.product_id = 2;

【讨论】:

  • 我们可以更新现有的数组吗?我们可以在 id 为 1 的订单的 products 数组中添加一个产品对象吗?
【解决方案2】:

在 12.2 中,您可以使用 JSON_EXISTS 执行此操作

 SQL> WITH ORDERS as
   2  (
   3    select 1 as ID, '{"products": [{ "product": 1, "quantity": 5}, {"product": 2, "quantity": 1}], "delivery_address": "My hometown"}' as DETAILS
   4      from dual
   5    union all
   6    select 2 as ID, '{"products": [{ "product": 42, "quantity": 1}, {"product": 10, "quantity": 2}], "comment": "Your website is too slow"}' as DETAILS
   7      from dual
   8    union all
   9    select 3 as ID, '{"products": [{ "product": 543, "quantity": 1}], "discount": "15"}' as DETAILS
  10      from dual
  11    union all
  12    select 4 as ID, '{"products": [{ "product": 738, "quantity": 12}], "discount": "32"}' as DETAILS
  13     from dual
  14  )
  15  select *
  16    from ORDERS
  17   where JSON_EXISTS(DETAILS,'$?(@.products.product == $PRODUCT)' passing 2 as "PRODUCT")
  18  /

         ID
 ----------
 DETAILS
 --------------------------------------------------------------------------------
          1
 {"products": [{ "product": 1, "quantity": 5}, {"product": 2, "quantity": 1}], "d
 elivery_address": "My hometown"}


 SQL>

【讨论】:

  • 实际上 json_exist 在 12.1 中以相同的方式工作,但不使用 indexex
猜你喜欢
  • 1970-01-01
  • 2016-01-15
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多