【问题标题】:SQL OPENJSON read nested JSON with empty/null arraySQL OPENJSON 读取带有空/空数组的嵌套 JSON
【发布时间】: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


    【解决方案1】:

    解决方案是OUTER APPLY on 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
        )
    OUTER APPLY OPENJSON(Discount,'$')
    WITH (
        nDiscount DECIMAL(19,6) '$.amount'
        )
    )
    

    结果完全符合预期:

    cOrderID     cSKU          nQty          nPrice          nDiscount
    ------------ ------------- ------------- --------------- -----------------
    1            abc           1.000000      100             10.000000
    2            def           1.000000      111             null
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多