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
?
3
Answers
I ended up using
JSON_TABLE
, and following worked:One of the options is to use CASE expression:
Oracle supports filtering in JSON Path literal, you may use it to restrict array items.
"key1": [
{
"subkey1":"somevalue1",
"subkey2":"somevalue2",
},
{
"subkey1":"interestedvalue1",
"subkey2":"interestedvalueToParseAndGet",
}
]
}
fiddle