【发布时间】:2020-04-30 01:07:40
【问题描述】:
我有一个带有嵌套数组的简单 JSON,其中一些可能为空或 null,由于 null 值,我无法得到整个结果。
declare @json nvarchar(max)
set @json = '{"orders": [{
"id": 1,
"items":
[{
"sku": "abc",
"quantity": 1,
"price": 100,
"discount":
[{
"amount": "10",
"amount_set":
{
"shop":
{
"total": "10",
"currency_code": "EUR"
},
"presentment":
{
"total": "10",
"currency_code": "EUR"
}
}
}]
}]
},{
"id": 2,
"items":
[{
"sku": "def",
"quantity": 1,
"price": 111,
"discount": []
}]
}
] }'
SELECT cOrderID, cSKU, nQty, nPrice, nDiscount
FROM
(
OPENJSON(@json, '$.orders')
WITH (
cOrderID NVARCHAR(20) '$.id',
Items NVARCHAR(MAX) '$.items' AS JSON
)
CROSS APPLY OPENJSON(Items,'$')
WITH (
cSKU NVARCHAR(30) '$.sku',
nQty DECIMAL(19,6) '$.quantity',
nPrice FLOAT '$.price',
Discount NVARCHAR(MAX) '$.discount' AS JSON
)
CROSS APPLY OPENJSON(Discount,'$')
WITH (
nDiscount DECIMAL(19,6) '$.amount'
)
)
我得到这个结果:
cOrderID cSKU nQty nPrice nDiscount
------------ ------------- ------------- --------------- -----------------
1 abc 1.000000 100 10.000000
我希望得到这样的结果:
cOrderID cSKU nQty nPrice nDiscount
------------ ------------- ------------- --------------- -----------------
1 abc 1.000000 100 10.000000
2 def 1.000000 111 null
如何显示包含空数组的行?
【问题讨论】:
标签: sql arrays json tsql nested