I have created a table
CREATE TABLE json_data(
json_col VARCHAR2(1000),
CONSTRAINT must_be_json CHECK (json_col IS JSON )
)
Inserted Data into table
INSERT INTO json_data VALUES ('{ "abc" : { "fname" : "john" ,"lname" : "rambo" }}')
INSERT INTO json_data VALUES ('{ "abc" : { "fname" : "james" ,"lname" : "bond" }}')
Need to select value using below query
SELECT json_col FROM json_data
WHERE JSON_EXISTS(json_col, '$.abc')
AND JSON_EXISTS(json_col,'$?(@.abc.name == "john")')
works fine without problem, however I need to pass value "John" as parameter like..
SELECT json_col FROM json_data
WHERE JSON_EXISTS(json_col, '$.abc')
AND JSON_EXISTS(json_col,'$?(@.abc.name == :johnParam)'
where :johnParam
is a parameter
its not taking as parameter, please help me how to pass :johnParam
parameter with double quotes
If you want to pass a variable to a JSON path in json_exists
, use the passing
clause:
create table json_data (
json_col varchar2(1000)
check ( json_col is json )
);
insert into json_data
values ('{ "abc" : { "fname" : "john" ,"lname" : "rambo" }}');
insert into json_data
values ('{ "abc" : { "fname" : "james" ,"lname" : "bond" }}');
select json_col from json_data
where json_exists (
json_col,
'$?(@.abc.fname == $johnparam)'
passing 'john' as "johnparam"
);
JSON_COL
{ "abc" : { "fname" : "john" ,"lname" : "rambo" }}
'john'
in the passing
clause becomes :johnParam
in your application.
To search for documents where an attribute equals a specific value, you may find it easier to use simple dot-notation or json_value
instead:
select * from json_data j
where j.json_col.abc.fname = 'john';
JSON_COL
{ "abc" : { "fname" : "john" ,"lname" : "rambo" }}
select * from json_data j
where json_value ( json_col, '$.abc.fname' ) = 'john';
JSON_COL
{ "abc" : { "fname" : "john" ,"lname" : "rambo" }}
Then you can replace 'john'
with :johnParam
as you would for queries against non-JSON data.
You can use substitution parameter with ampersand such as &name
with the following query
SELECT json_col
FROM json_data
WHERE JSON_EXISTS(json_col, '$.abc?(@.fname == "&name")')
where the part JSON_EXISTS(json_col, '$.abc')
seems redundant. When prompted, enter john
without quotes as value for $name
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.