I have never worked with JSON in SQL Server before that's why need some help.
I have written a simple snippet of code:
DECLARE @json NVARCHAR(4000)
SET @json =
N'{
"id":"40476",
"tags":[
{
"id":"5f5883",
},
{
"id":"5fc8",
}
],
"type":"student",
"external_id":"40614476"
}'
SELECT
JSON_value(@json, '$.tags[0].id') as tags
In sample above I write code how get first "id" from "tags".
But how looks like script if in "tags" not 2 "id", but an unknown number this "id" and result should be in column like this:
2条答案
按热度按时间hlswsv351#
You may use
OPENJSON()
with explicit schema to parse the$.tags
JSON array:Result:
If you want to get the index of each
id
in the$.tags
JSON array, then you need a combination ofOPENJSON()
with default schema andJSON_VALUE()
:Result:
j1dl9f462#