【发布时间】:2018-09-01 10:43:41
【问题描述】:
拥有这样的 JSON(我知道 JSON 不支持 cmets。在本例中用于说明这个想法):
{
"people": [
{ --// <-- index 0
"id": 100,
"name": "John Doe"
},
{ --// <-- index 1
"id": 101,
"name": "Jane Roe"
}
]
}
我们可以像这样从数组中的特定元素中选择值:
SELECT name
FROM JSON_TABLE(
'{
"people": [
{
"id": 100,
"name": "John Doe"
},
{
"id": 101,
"name": "Jane Roe"
},
]
}', '$.people[*]'
COLUMNS(
ID NUMBER PATH '$.id',
NAME VARCHAR2 PATH '$.name'
)
) info
WHERE info.id = 101
结果:
NAME
--------
Jane Roe
有没有办法获取数组中的元素索引?比如:
SELECT array_index --// <-- how get the array index of the element found?
FROM JSON_TABLE(
--// ...
) info
WHERE info.id = 101
结果:
ARRAY_INDEX
-----------
1
是否可以使用 Oracle 12c 中的 JSON 支持来做这样的事情?
【问题讨论】:
-
一个 JSON 被解析成关系记录。关系记录没有数组索引。您可以使用 rownum 为所选记录分配编号。