在plsql中从json数组中选择多个值

问题描述 投票:0回答:2

我想从 json 数组中选择值。例如: 数据位于表数据中,列名称为 json_col

{
    "ABlock": {
        "fruits1": [{
            "Frt1": "Apple",
            "Clr1": "Red",
            "Qty1": "14"
        }, {
            "Frt1": "Grapes",
            "Clr1": "Black",
            "Qty1": "7"
        }],
        "fruits2": [{
            "Frt2": "Pear",
            "Clr2": "Green",
            "Qty2": "9"
        }, {
            "Frt2": "Lemon",
            "Clr2": "Yellow",
            "Qty2": "5"
        }]
    }
}

这里我要选择Qty1&Qty2。 我尝试仅选择 Qty1 的代码是

Select json_value(json_col, '$.ABlock.fruits1[0].Qty1) + ',' + json_value(json_col, '$.ABlock.fruits1[1].Qty1) as qty 
  from Data;

但是我收到错误“无效号码”

我应该得到的输出是: 14 7.

arrays json plsql
2个回答
1
投票

假设这是oracle,连接符号是“||”,而不是“+”。无效数字是因为您尝试将值“14”、“、”和“7”相加,结果为:

SELECT 14 + ',' + 7 from dual;

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

这有效:

with json_doc AS 
(SELECT
'{
    "ABlock": {
        "fruits1": [{
            "Frt1": "Apple",
            "Clr1": "Red",
            "Qty1": "14"
        }, {
            "Frt1": "Grapes",
            "Clr1": "Black",
            "Qty1": "7"
        }],
        "fruits2": [{
            "Frt2": "Pear",
            "Clr2": "Green",
            "Qty2": "9"
        }, {
            "Frt2": "Lemon",
            "Clr2": "Yellow",
            "Qty2": "5"
        }]
    }
}' AS json_col FROM dual
)
SELECT
json_value(json_col, '$.ABlock.fruits1[0].Qty1') ||' '||
json_value(json_col, '$.ABlock.fruits1[1].Qty1') 
FROM json_doc;

14 7

||' '||
替换为
||','||
得到 14,7


0
投票

如何在不传递索引的情况下选择数组中的所有值,因为如果您不知道数组的大小,我们如何获取值?

© www.soinside.com 2019 - 2024. All rights reserved.