英文:
Oracle SQL conditionally parse a json column
问题
如果我执行以下操作:
select JSON_VALUE(column1, '$.key1[1].subkey2') from table1
我可以从查询中获得interestedvalueToParseAndGet
,这是我期望的结果。
但是,我不能保证这将始终是数组中的第二个项目。
如何有条件地从数组中获取项目(其subkey1
为interestedvalue1
),并获取其对应的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 |
英文:
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 |
答案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, '{
"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
答案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,
'$.key1[*]'
COLUMNS (
subkey1 VARCHAR2(100) PATH '$.subkey1',
subkey2 VARCHAR2(100) PATH '$.subkey2'
)
)
WHERE subkey1 = 'interestedvalue1';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论