【发布时间】:2021-12-05 21:30:46
【问题描述】:
在 SQL Server 2016 中,我正在尝试转换以下 JSON:
DECLARE @json NVARCHAR(MAX);
SET @json =
N' {
"date": "2021-12-31",
"distributor_name": "Test",
"movies": [
{
"category_id": 3,
"name": "Dune",
"budget": 165,
"release_date": "2021-09-03",
"location": [
{
"location_type": 1,
"location_code": "US"
},
{
"location_type": 2,
"location_code": "CA"
},
{
"location_type": 2,
"location_code": "UK"
}
]
},
{
"category_id": 2,
"name": "No Time to Die",
"budget": 250,
"release_date": "2021-09-28",
"location": [
{
"location_type": 1,
"location_code": "US"
},
{
"location_type": 1,
"location_code": "UK"
}
]
}
]
}
';
进入:
| category_id | name | budget | release_date | country | distribution |
|---|---|---|---|---|---|
| 3 | Dune | 165 | 2021-09-03 | US | CA, UK |
| 2 | No Time to Die | 250 | 2021-09-28 | US, UK | NULL |
应在以下语句中添加什么以确保所有location_code 和location_type = 1 以逗号分隔的列表格式位于country 下,并且所有location_code 和location_type = 2 都位于distribution 下逗号分隔的列表格式。
SELECT *
FROM OPENJSON(@json, '$.movies')
WITH (
category_id INT '$.category_id',
name VARCHAR(255) '$.name',
budget INT '$.budget',
release_date DATE '$.release_date'
)
【问题讨论】:
-
您的 SQL Server 版本是多少?
-
@YitzhakKhabinsky 13.0.5888.11
标签: json sql-server open-json