使用T-SQL从JSON中提取

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

我正在尝试从以下 JSON 中提取数据,并将“行”中的数据转换为列。

{
"tables": [
    {
        "name": "PrimaryResult",
        "columns": [
            {
                "name": "timestamp",
                "type": "datetime"
            },
            {
                "name": "message",
                "type": "string"
            },
            {
                "name": "severityLevel",
                "type": "int"
            },
            {
                "name": "FriendlySeverityLevel",
                "type": "string"
            },
            {
                "name": "CycleId",
                "type": "dynamic"
            }
        ],
        "rows": [
            [
                "2024-07-25T12:00:00",
                "Report Started - User: [email protected] MrmClient: 400600 CcID: 000a000b-0000-111c-0a0b-11111a1111a1",
                1,
                "Information",
                "000a000b-0000-111c-0a0b-11111a1111a1"
            ],
            [
                "2024-07-24T12:00:00",
                "Report Started - User: [email protected] MrmClient: 807231 CcID: 000c000d-0000-11c1-0c0d-11111b11111b",
                1,
                "Information",
                "000c000d-0000-11c1-0c0d-11111b11111b"
            ]
        ]
    }
]

}

这就是我想要得到的最终结果。

enter image description here

使用

JSON_VALUE(RawJSON, '$.tables[0].name')
给了我 PrimaryResult 但我不知道如何从“行”中提取数据。 我尝试过
JSON_VALUE(RawJSON, '$.tables[0].rows[0]')
但它返回 NULL。 任何帮助将不胜感激。

sql-server t-sql
1个回答
0
投票

在 ssms/azure data studio 中尝试一下:

DECLARE @jsonInfo NVARCHAR(MAX)
SET @jsonInfo = N'{
"tables": [
    {
        "name": "PrimaryResult",
        "columns": [
            {
                "name": "timestamp",
                "type": "datetime"
            },
            {
                "name": "message",
                "type": "string"
            },
            {
                "name": "severityLevel",
                "type": "int"
            },
            {
                "name": "FriendlySeverityLevel",
                "type": "string"
            },
            {
                "name": "CycleId",
                "type": "dynamic"
            }
        ],
        "rows": [
            [
                "2024-07-25T12:00:00",
                "Report Started - User: [email protected] MrmClient: 400600 CcID: 000a000b-0000-111c-0a0b-11111a1111a1",
                1,
                "Information",
                "000a000b-0000-111c-0a0b-11111a1111a1"
            ],
            [
                "2024-07-24T12:00:00",
                "Report Started - User: [email protected] MrmClient: 807231 CcID: 000c000d-0000-11c1-0c0d-11111b11111b",
                1,
                "Information",
                "000c000d-0000-11c1-0c0d-11111b11111b"
            ]
        ]
    }
]
}'

SELECT JSON_VALUE(@jsonInfo, '$.tables[0].rows[0][0]') as timestamp;
SELECT JSON_VALUE(@jsonInfo, '$.tables[0].rows[0][1]') as message;
© www.soinside.com 2019 - 2024. All rights reserved.