Skip to content
Advertisement

Using JSON_VALUE for parse column in SQL Server table

I have never worked with JSON in SQL Server before that’s why need some help.

I have written a simple snippet of code:

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:

Advertisement

Answer

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 of OPENJSON() with default schema and JSON_VALUE():

Result:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement