Oracle SQL条件性解析JSON列

huangapple go评论71阅读模式
英文:

Oracle SQL conditionally parse a json column

问题

如果我执行以下操作:

select JSON_VALUE(column1, '$.key1[1].subkey2') from table1

我可以从查询中获得interestedvalueToParseAndGet,这是我期望的结果。

但是,我不能保证这将始终是数组中的第二个项目。

如何有条件地从数组中获取项目(其subkey1interestedvalue1),并获取其对应的subkey2的值呢?

英文:

in a DB table, table1 I have the following JSON in column1:

{
  "key1": [
    {
      "subkey1":"somevalue1",
      "subkey2":"somevalue2",
    },
    {
      "subkey1":"interestedvalue1",
      "subkey2":"interestedvalueToParseAndGet",
    }
  ]
}

If I do the following,

select JSON_VALUE(column1, '$.key1[1].subkey2') from table1

I'm able to get interestedvalueToParseAndGet from the query, which is what I'm expecting.

But I cannot guarantee that this will be the second item in the array always.

How to conditionally fetch the item from the array (who's subkey1 is interestedvalue1) and get its corresponding value for subkey2?

答案1

得分: 3

Oracle支持在JSON Path字面量中进行过滤,您可以使用它来限制数组项。

with sample (val) as (
  select '{ 
    "key1": [
      {
        "subkey1":"somevalue1",
        "subkey2":"somevalue2",
      },
      {
        "subkey1":"interestedvalue1",
        "subkey2":"interestedvalueToParseAndGet",
      }
    ]
  }'
  from dual
)
select
  sample.*,
  json_value(val, '$.key1[*]?(@.subkey1 == "interestedvalue1").subkey2') as subkey2
from sample
VAL SUBKEY2
{"key1": [{"subkey1":"somevalue1","subkey2":"somevalue2"},{"subkey1":"interestedvalue1","subkey2":"interestedvalueToParseAndGet"}]} interestedvalueToParseAndGet

fiddle

英文:

Oracle supports filtering in JSON Path literal, you may use it to restrict array items.

with sample (val) as (
  select '{
  "key1": [
    {
      "subkey1":"somevalue1",
      "subkey2":"somevalue2",
    },
    {
      "subkey1":"interestedvalue1",
      "subkey2":"interestedvalueToParseAndGet",
    }
  ]
}'
  from dual
)
select
  sample.*,
  json_value(val, '$.key1[*]?(@.subkey1 == "interestedvalue1").subkey2') as subkey2
from sample
VAL SUBKEY2
{<br>  "key1": [<br>    {<br>      "subkey1":"somevalue1",<br>      "subkey2":"somevalue2",<br>    },<br>    {<br>      "subkey1":"interestedvalue1",<br>      "subkey2":"interestedvalueToParseAndGet",<br>    }<br>  ]<br>} interestedvalueToParseAndGet

fiddle

答案2

得分: 1

使用CASE表达式之一的选项是:

WITH            -- Sample Data:
    tbl (ID, KEYS) AS
        (   Select 1, '{
                          "key1": [
                            {
                              "subkey1":"somevalue1",
                              "subkey2":"somevalue2",
                            },
                            {
                              "subkey1":"interestedValue1",
                              "subkey2":"interestedValueFromSecond",
                            }
                          ]
                        }' 
           From Dual Union All
           Select 2, '{
                          "key1": [
                            {
                              "subkey1":"interestedValue1",
                              "subkey2":"interestedValueFromFirst",
                            },
                            {
                              "subkey1":"someOtherValue",
                              "subkey2":"notinterested",
                            }
                          ]
                        }' 
           From Dual
      )
--  M a i n   S Q L :
Select ID, CASE WHEN JSON_VALUE(KEYS, '$.key1[0].subkey1') = 'interestedValue1' 
                THEN JSON_VALUE(KEYS, '$.key1[0].subkey2')
                WHEN JSON_VALUE(KEYS, '$.key1[1].subkey1') = 'interestedValue1' 
                THEN JSON_VALUE(KEYS, '$.key1[1].subkey2')
           END "INTERESTED_VALUE" 
From tbl
--
--  R e s u l t :
--  ID  INTERESTED_VALUE
--  --  --------------------------
--   1  interestedValueFromSecond
--   2  interestedValueFromFirst

请注意,代码部分未进行翻译。

英文:

One of the options is to use CASE expression:

WITH            -- Sample Data:
    tbl (ID, KEYS) AS
        (   Select 1, &#39;{
                          &quot;key1&quot;: [
                            {
                              &quot;subkey1&quot;:&quot;somevalue1&quot;,
                              &quot;subkey2&quot;:&quot;somevalue2&quot;,
                            },
                            {
                              &quot;subkey1&quot;:&quot;interestedValue1&quot;,
                              &quot;subkey2&quot;:&quot;interestedValueFromSecond&quot;,
                            }
                          ]
                        }&#39; 
           From Dual Union All
           Select 2, &#39;{
                          &quot;key1&quot;: [
                            {
                              &quot;subkey1&quot;:&quot;interestedValue1&quot;,
                              &quot;subkey2&quot;:&quot;interestedValueFromFirst&quot;,
                            },
                            {
                              &quot;subkey1&quot;:&quot;someOtherValue&quot;,
                              &quot;subkey2&quot;:&quot;notinterested&quot;,
                            }
                          ]
                        }&#39; 
           From Dual
      )
--  M a i n   S Q L :
Select ID, CASE WHEN JSON_VALUE(KEYS, &#39;$.key1[0].subkey1&#39;) = &#39;interestedValue1&#39; 
                THEN JSON_VALUE(KEYS, &#39;$.key1[0].subkey2&#39;)
                WHEN JSON_VALUE(KEYS, &#39;$.key1[1].subkey1&#39;) = &#39;interestedValue1&#39; 
                THEN JSON_VALUE(KEYS, &#39;$.key1[1].subkey2&#39;)
           END &quot;INTERESTED_VALUE&quot; 
From tbl
--
--  R e s u l t :
--  ID  INTERESTED_VALUE
--  --  --------------------------
--   1	interestedValueFromSecond
--   2	interestedValueFromFirst

答案3

得分: 0

我最终使用了JSON_TABLE,以下内容有效:

SELECT subkey2 
FROM table1,
JSON_TABLE(
   json_data,
   '$.key1[*]'
   COLUMNS (
     subkey1 VARCHAR2(100) PATH '$.subkey1',
     subkey2 VARCHAR2(100) PATH '$.subkey2'
   )
)
WHERE subkey1 = 'interestedvalue1';
英文:

I ended up using JSON_TABLE, and following worked:

SELECT subkey2 
FROM table1,
JSON_TABLE(
json_data,
&#39;$.key1[*]&#39;
COLUMNS (
subkey1 VARCHAR2(100) PATH &#39;$.subkey1&#39;,
subkey2 VARCHAR2(100) PATH &#39;$.subkey2&#39;
)
)
WHERE subkey1 = &#39;interestedvalue1&#39;;

huangapple
  • 本文由 发表于 2023年6月1日 11:58:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定