正在从 Oracle table 查询 JSON 数据字段

Querying JSON data field from Oracle table

甲骨文: 我的“TESTREPAIR”table 中有一个字段,其中一列“DETAILS”存储以下内容 JSON。

{
    "repairActions": [
        {
            "symptom": 524,
            "defect": "Defective Component",
            "action": "Reflow"
        },
        {
            "symptom": 506,
            "defect": "Cosmetic",
            "action": "Repaired Component"
        },
        {
            "symptom": 509,
            "defect": "Defective Component",
            "action": "Swapped"
        }
    ]
}

我正在使用 SELECT 语句从上面的 JSON 数据字段查询症状。

像这样。

SELECT
    r.details.repairactions.symptom[0],
    r.details.repairactions.symptom[1],
    r.details.repairactions.symptom[2]
FROM
    testrepair r;

以上查询返回空值。我想要的输出是

symptom1: 524
symptom2: 506
symptom3: 509

您想将数组索引放在正确的位置,并使用与 JSON:

大小写匹配的带引号标识符
SELECT r.details."repairActions"[0]."symptom" AS symptom1,
       r.details."repairActions"[1]."symptom" AS symptom2,
       r.details."repairActions"[2]."symptom" AS symptom3
FROM   testrepair r;

其中,对于示例数据:

CREATE TABLE testrepair (details JSON);

INSERT INTO testrepair (details)
VALUES ('{
    "repairActions": [
        {
            "symptom": 524,
            "defect": "Defective Component",
            "action": "Reflow"
        },
        {
            "symptom": 506,
            "defect": "Cosmetic",
            "action": "Repaired Component"
        },
        {
            "symptom": 509,
            "defect": "Defective Component",
            "action": "Swapped"
        }
    ]
}');

输出:

SYMPTOM1 SYMPTOM2 SYMPTOM3
524 506 509

db<>fiddle here