Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

query json array having only strings oracle

Below is the json stored in a table called "Sample" and the column name is "argument". I want to fetch all those records having a particular value in a specified argument. I could query the argument name but not able to query a particular value as it is an array of strings. (Please find my keys have . in it)

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

This gives me all the records having particular argument.

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

But I need to match argument value. I tried below but getting JSON expression error.

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

Please help. Thanks in advance.

question from:https://stackoverflow.com/questions/65876377/query-json-array-having-only-strings-oracle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have the quotation marks in the wrong place; you want the double quotes before the square-brackets for the array instead of afterwards:

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

Which, for the sample data:

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"]
 }
}');

Outputs:

| ARGUMENT                                                                                                                 |
| :----------------------------------------------------------------------------------------------------------------------- |
| {<br> "arguments":{<br>    "app.argument1.appId":["123", "456"],<br>    "app.argument2.testId":["546", "567"]<br> }<br>} |

db<>fiddle here


Do you know a way to do this in 12.1?

You could also use EXISTS with a correlated 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<>fiddle here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...