使用 OPENJSON 跳过 OBJECTS 解析 JSON SQL
Parse JSON SQL with OPENJSON skipping OBJECTS
我正在尝试在 Azure sql 中使用 OPENJSON 解析 json 文件,但我想在我的新 table 中进行特定映射。
我有一个 json 文件,看起来像这样,其中包含许多 y0_1、y0_2、....y0_380.
{"proto": "01",
"type": "A",
"description": "heartbeat",
"geometry": {"y0_1": {"tag": "Normal",
"probability": 0.40,
"x": 39,
"y": 13},
"y0_2": {"tag": "category_3", "probability": 0.8, "x": 48, "y": 13},
"y0_3": {"tag": "Normal", "probability": 0.9, "x": 27, "y": 10},
"Test": {"proba": 0.65}}}
我想解析 json 并有一个 table 使用这种格式:
我尝试使用 Openjson 格式解析 json,但我不知道如何像 link[= 中的图片那样在同一列中获取所有 y 和 x 15=]
SELECT * from OPENROWSET (
BULK 'sample.json',
DATA_SOURCE ='dataset',
FORMATFILE_DATA_SOURCE = 'myblobstorage',
SINGLE_CLOB
) AS J
CROSS APPLY OPENJSON (BulkColumn)
WITH (
proto nvarchar(100) N'$.proto',
description nvarchar(100) N'$.description',
)
table output desired
使用OPENJSON
、AS JSON
、data-type和CROSS APPLY
钻取JSON的不同层。我创建了一个简化示例来展示该技术:
DECLARE @json NVARCHAR(MAX) = '{
"proto": "01",
"type": "A",
"description": "heartbeat",
"geometry": {
"y0_1": {
"tag": "Normal",
"probability": 0.40,
"x": 39,
"y": 13
},
"y0_2": {
"tag": "category_3",
"probability": 0.8,
"x": 48,
"y": 13
},
"y0_3": {
"tag": "Normal",
"probability": 0.9,
"x": 27,
"y": 10
},
"Test": {
"proba": 0.65
}
}
}'
SELECT
j.proto,
j.[type],
j.[description],
JSON_VALUE ( g.[value], '$.tag' ) AS tag,
JSON_VALUE ( g.[value], '$.probability' ) AS probability,
JSON_VALUE ( g.[value], '$.x' ) AS x,
JSON_VALUE ( g.[value], '$.y' ) AS y
FROM OPENJSON( @json, '$' )
WITH (
proto VARCHAR(50),
[type] VARCHAR(50),
[description] VARCHAR(50),
[geometry] NVARCHAR(MAX) AS JSON
) j
CROSS APPLY OPENJSON ( [geometry] ) g
我的结果:
我正在尝试在 Azure sql 中使用 OPENJSON 解析 json 文件,但我想在我的新 table 中进行特定映射。 我有一个 json 文件,看起来像这样,其中包含许多 y0_1、y0_2、....y0_380.
{"proto": "01",
"type": "A",
"description": "heartbeat",
"geometry": {"y0_1": {"tag": "Normal",
"probability": 0.40,
"x": 39,
"y": 13},
"y0_2": {"tag": "category_3", "probability": 0.8, "x": 48, "y": 13},
"y0_3": {"tag": "Normal", "probability": 0.9, "x": 27, "y": 10},
"Test": {"proba": 0.65}}}
我想解析 json 并有一个 table 使用这种格式: 我尝试使用 Openjson 格式解析 json,但我不知道如何像 link[= 中的图片那样在同一列中获取所有 y 和 x 15=]
SELECT * from OPENROWSET (
BULK 'sample.json',
DATA_SOURCE ='dataset',
FORMATFILE_DATA_SOURCE = 'myblobstorage',
SINGLE_CLOB
) AS J
CROSS APPLY OPENJSON (BulkColumn)
WITH (
proto nvarchar(100) N'$.proto',
description nvarchar(100) N'$.description',
)
table output desired
使用OPENJSON
、AS JSON
、data-type和CROSS APPLY
钻取JSON的不同层。我创建了一个简化示例来展示该技术:
DECLARE @json NVARCHAR(MAX) = '{
"proto": "01",
"type": "A",
"description": "heartbeat",
"geometry": {
"y0_1": {
"tag": "Normal",
"probability": 0.40,
"x": 39,
"y": 13
},
"y0_2": {
"tag": "category_3",
"probability": 0.8,
"x": 48,
"y": 13
},
"y0_3": {
"tag": "Normal",
"probability": 0.9,
"x": 27,
"y": 10
},
"Test": {
"proba": 0.65
}
}
}'
SELECT
j.proto,
j.[type],
j.[description],
JSON_VALUE ( g.[value], '$.tag' ) AS tag,
JSON_VALUE ( g.[value], '$.probability' ) AS probability,
JSON_VALUE ( g.[value], '$.x' ) AS x,
JSON_VALUE ( g.[value], '$.y' ) AS y
FROM OPENJSON( @json, '$' )
WITH (
proto VARCHAR(50),
[type] VARCHAR(50),
[description] VARCHAR(50),
[geometry] NVARCHAR(MAX) AS JSON
) j
CROSS APPLY OPENJSON ( [geometry] ) g
我的结果: