Select 来自 plsql 中 json 数组的多个值
Select multiple values from a json array in plsql
我想从 json 数组中获取 select 值。例如:
数据在table列名中的数据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"
}]
}
}
这里我要select Qty1&Qty2。
我尝试 select 的代码只是 Qty1 是
Select json_value(json_col, '$.ABlock.fruits1[0].Qty1) + ',' + json_value(json_col, '$.ABlock.fruits1[1].Qty1) as qty
from Data;
但我收到错误 'invalid number'
我应该得到的输出是:
14
7.
假设这是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
我想从 json 数组中获取 select 值。例如: 数据在table列名中的数据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"
}]
}
}
这里我要select Qty1&Qty2。 我尝试 select 的代码只是 Qty1 是
Select json_value(json_col, '$.ABlock.fruits1[0].Qty1) + ',' + json_value(json_col, '$.ABlock.fruits1[1].Qty1) as qty
from Data;
但我收到错误 'invalid number'
我应该得到的输出是: 14 7.
假设这是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