oracle读取json响应

byqmnocz  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(511)

我有下一个问题。进行此查询:

select apex_web_service.make_rest_request(
     p_url => 'https://something.com/rest/Discount'
     ,p_http_method => 'GET'
     ,p_username => 'username'
     ,p_password => 'password'
    ) as custom
from dual;

返回以下内容:

{"hasMore":false,"items":[{"id":12,"Origin":"ALL","Part":"PO423S","Channel":"RC"},{"id":13,"Origin":"ALL","Part":"LO123D","Channel":"RC"},{"id":14,"Origin":"ALL","Part":"SD765S","Channel":"AP"}]}

我想做一个频道组,看看有多少频道我必须插入到另一个表。
我试着列出:

select d.custom.items
from  (
    select apex_web_service.make_rest_request(
        p_url =>  'https://something.com/rest/Discount'
       ,p_http_method => 'GET'
       ,p_username => 'username'
       ,p_password => 'password'
    ) as custom
    from dual) d;

但我有个错误:

ORA-22806: no es un objeto ni un elemento REF
22806. 00000 -  "not an object or REF"

* Cause:    An attempt was made to extract an attribute from an item that is

           neither an object nor a REF.

* Action:   Use an object type or REF type item and retry the operation.

Error en la línea: 12, columna: 8

我还测试下一个:

create table temp_json (
  json_data blob not null
);
alter table temp_json
  add constraint temp_data_json 
  check ( json_data is json );

insert into temp_json
select apex_web_service.make_rest_request(
     p_url =>  'https://something.com/rest/Discount'
    ,p_http_method => 'GET'
    ,p_username => 'username'
    ,p_password => 'password
    ) as customDiscAplicability
from dual
;

select d.json_data.items
from   temp_json d;

结果是:

ITEMS
-----
(null)

我遵循这个教程:链接
有人能帮我吗?
当做

nwlls2ji

nwlls2ji1#

当您选择 apex_web_service.make_rest_request ,它返回一个字符串。数据库不知道这是json数据。
如果您使用的是oracle数据库18c或更高版本,那么应该可以使用 treat ... as json :

select d.custom.items.id from (
select treat ( '{
  "hasMore": false,
  "items": [  {
      "id": 12,
      "Origin": "ALL",
      "Part": "PO423S",
      "Channel": "RC"
    },  {
      "id": 13,
      "Origin": "ALL",
      "Part": "LO123D",
      "Channel": "RC"
    },  {
      "id": 14,
      "Origin": "ALL",
      "Part": "SD765S",
      "Channel": "AP"
    }
  ]
}' as json ) custom from dual
) d;

ITEMS        
[12,13,14]

为了理解为什么将响应插入表中,然后选择它返回null,我们需要查看其中的确切json!

相关问题