Sql 提供空值的OPENJSON

Sql 提供空值的OPENJSON,sql,json,sql-server,tsql,sql-server-2016,Sql,Json,Sql Server,Tsql,Sql Server 2016,我正在使用SQL Express Edition 2016 这是我的问题。它正在返回空值 Declare @JSON varchar(max) SELECT @JSON = BulkColumn FROM OPENROWSET (BULK 'F:\JKPM\18Aug2018\Mod1.json', SINGLE_CLOB) as j SELECT * FROM OPENJSON (@JSON) WITH(transactionId nvarchar(50)) 我的JSON是: {

我正在使用SQL Express Edition 2016

这是我的问题。它正在返回空值

Declare @JSON varchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK 'F:\JKPM\18Aug2018\Mod1.json', SINGLE_CLOB) as j

SELECT * FROM OPENJSON (@JSON) 
WITH(transactionId nvarchar(50)) 
我的JSON是:

{
    "data": {
        "deviceTransactions": [{
            "transactionId": "3f147089-ff52-5552-a29b-cd32a7e2ba97",
            "organizationId": "9023179c-7888-4c5e-a831-28259b8a8872",
            "device": "JTLSPERF1",
            "event": "PUSH",
            "siteName": "TEST-SITE",
            "siteId": "5555f068-3ed2-4f83-b084-666f4f92734",
            "createdTime": "Sat, 18 Aug 2018 13:51:57 GMT",
            "updatedTime": "Sat, 18 Aug 2018 13:53:07 GMT",
            "userName": "test1",
            "userId": "test1",
            "status": "Failed",
            "details": {
                "count": 251,
                "deviceId": "44343570-7eb5-11e8-8160-057818f1057b",
                "error": {
                    "errorMessage": "Failed to start polling status of the Queue",
                    "stage": "MANAGE_PUSH",
                    "errorCode": "ERR021001"
                },
                "jcigId": "4rtr5aa6a2-71b5-455f-8890-7e2aa398b757"
            },
            "cancelledBy": ""
        }],
        "libraryTransactions": []
    },
    "success": true,
    "statusCode": 200
}

transactionId是deviceTransactions的一部分,deviceTransactions也是一个数组。下面是一个在这种情况下如何使用openjson的示例:

    DECLARE @json NVARCHAR(MAX);

    SET @json = N'
    {
        "data": {
            "deviceTransactions": [{
                "transactionId": "3f147089-ff52-5552-a29b-cd32a7e2ba97",
                "organizationId": "9023179c-7888-4c5e-a831-28259b8a8872",
                "device": "JTLSPERF1",
                "event": "PUSH",
                "siteName": "TEST-SITE",
                "siteId": "5555f068-3ed2-4f83-b084-666f4f92734",
                "createdTime": "Sat, 18 Aug 2018 13:51:57 GMT",
                "updatedTime": "Sat, 18 Aug 2018 13:53:07 GMT",
                "userName": "test1",
                "userId": "test1",
                "status": "Failed",
                "details": {
                    "count": 251,
                    "deviceId": "44343570-7eb5-11e8-8160-057818f1057b",
                    "error": {
                        "errorMessage": "Failed to start polling status of the Queue",
                        "stage": "MANAGE_PUSH",
                        "errorCode": "ERR021001"
                    },
                    "jcigId": "4rtr5aa6a2-71b5-455f-8890-7e2aa398b757"
                },
                "cancelledBy": ""
            }],
            "libraryTransactions": []
        },
        "success": true,
        "statusCode": 200
    }
    ';

    --This will get you just the transactionId
    SELECT *
    FROM   OPENJSON(@json, '$.data.deviceTransactions')
               WITH (
                        [transactionId] NVARCHAR(200) '$.transactionId')

    --This gets you all the columns                 
    SELECT *
    FROM   OPENJSON(@json, '$.data.deviceTransactions')
               WITH (
                        [transactionId] NVARCHAR(200) '$.transactionId'
                      , [organizationId] NVARCHAR(200) '$.organizationId'
                      , [device] NVARCHAR(200) '$.device'
                      , [event] NVARCHAR(200) '$.event'
                      , [siteName] NVARCHAR(200) '$.siteName'
                      , [createdTime] NVARCHAR(200) '$.createdTime'
                      , [updatedTime] NVARCHAR(200) '$.updatedTime'
                      , [userName] NVARCHAR(200) '$.userName'
                      , [userId] NVARCHAR(200) '$.userId'
                      , [status] NVARCHAR(200) '$.status'
                      , [count] NVARCHAR(200) '$.details.count'
                      , [deviceId] NVARCHAR(200) '$.details.deviceId'
                      , [errorMessage] NVARCHAR(MAX) '$.details.error.errorMessage'
                      , [errorStage] NVARCHAR(200) '$.details.error.stage'
                      , [errorCode] NVARCHAR(200) '$.details.error.errorCode'
                      , [jcigId] NVARCHAR(200) '$.details.jcigId'
                      , [cancelledBy] NVARCHAR(200) '$.cancelledBy'
                    )

transactionId
不是顶级属性。您需要
$.data.deviceTransactions.transactionId
(或者,如果可能有多个事务,则更可能需要更复杂的JSON数据分解)。请看上面的文档。谢谢。如何输出包含JSON片段的
详细信息的单个列,即:
{“count”:251,“deviceId”:“44343570-7eb5-11e8-8160-057818f1057b”,“error”:{“errorMessage”:“无法启动队列的轮询状态”“阶段”:“管理推送”,“错误代码”:“ERR021001”}