JSON_VALUE 是否可以使用通配符和范围?
Is it possible to use wildcards and ranges with JSON_VALUE?
我目前正在研究使用 Oracle 12c 的 JSON 特性的示例,并且我正在努力了解如何将通配符和范围与 JSON_VALUE 一起使用。
我正在使用的 example 来自 Oracle Magazine 的 January/February 2015 年问题,这是我坚持的 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
文章提到
...To show all
the values, Mark uses CheckDetails[*]. (He can use specific numbers to
pull specific items—for example, CheckDetails[0,2] to pull the first
and third items...
所以我期望查询的第二行和第三行 return 所有 CheckNumber 值(101 和 102)。
我正在使用 Oracle 数据库 12.1.0.2.0,这里是 table 并在需要时插入脚本:
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_VALUE
是指 return 标量。要 return JSON 对象或关系数据,您必须使用 JSON_QUERY
或 JSON_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
我目前正在研究使用 Oracle 12c 的 JSON 特性的示例,并且我正在努力了解如何将通配符和范围与 JSON_VALUE 一起使用。
我正在使用的 example 来自 Oracle Magazine 的 January/February 2015 年问题,这是我坚持的 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
文章提到
...To show all the values, Mark uses CheckDetails[*]. (He can use specific numbers to pull specific items—for example, CheckDetails[0,2] to pull the first and third items...
所以我期望查询的第二行和第三行 return 所有 CheckNumber 值(101 和 102)。
我正在使用 Oracle 数据库 12.1.0.2.0,这里是 table 并在需要时插入脚本:
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_VALUE
是指 return 标量。要 return JSON 对象或关系数据,您必须使用 JSON_QUERY
或 JSON_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