我有像JSON一样的
{
"url": {
"web_categories": [{ "id": 226 } , { "id": 401 }]
},
"account":"Basic"
}
我想在SQL中输出一个列,其中包含匹配的URL的所有web_categories
的ID。
| webcat |
|--------|
| 226 |
| 401 |
使用SQL Server 2016 / Azure SQL DB对JSON的支持,如何实现这一目标?
DECLARE @jsonInfo VARCHAR(MAX)
SET @jsonInfo =N'{
"url": {
"web_categories": [{ "id": 226 }]
},
"account":"Basic"
}'
select * from openjson(@jsonInfo) with (webcat varchar(12) '$.url.web_categories.id[0]')
select * from openjson(@jsonInfo) with (webcat varchar(12) '$.url.web_categories.id')
select * from openjson(@jsonInfo) with (webcat varchar(12) '$.url.web_categories')
select * from openjson(@jsonInfo,'$.url.web_categories.id[0]')
select * from openjson(@jsonInfo,'$.url.web_categories.id')
select * from openjson(@jsonInfo,'$.url.web_categories')
select JSON_QUERY(@jsonInfo, '$.url.web_categories.id[0]') webcat
select JSON_QUERY(@jsonInfo, '$.url.web_categories.id') webcat
select JSON_QUERY(@jsonInfo, '$.url.web_categories') webcat
select JSON_VALUE(@jsonInfo, '$.url.web_categories.id[0]') webcat
select JSON_VALUE(@jsonInfo, '$.url.web_categories.id') webcat
select JSON_VALUE(@jsonInfo, '$.url.web_categories') webcat
答案 0 :(得分:2)
使用默认架构,将返回包含value
列的记录,该列包含阵列中每个项目的JSON。然后需要处理此JSON。
DECLARE @jsonInfo VARCHAR(MAX)
SET @jsonInfo =N'{
"url": {
"web_categories": [{ "id": 226 },{ "id": 411 }]
},
"account":101
}'
SELECT JSON_VALUE(value,'$.id') AS ids FROM OPENJSON(@jsoninfo,'$.url.web_categories')