Skip to content

Instantly share code, notes, and snippets.

@fernandezja
Created March 29, 2024 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezja/1292eda7552f3158afdc1bb3cc09c7b1 to your computer and use it in GitHub Desktop.
Save fernandezja/1292eda7552f3158afdc1bb3cc09c7b1 to your computer and use it in GitHub Desktop.
Get value from a string as JSON data with TS-SQL in MSSQL (using JSON_QUERY or JSON_VALUE)
--------------------------------------------------------------------------------
-- Example JSON_QUERY JSON_VALUE
--------------------------------------------------------------------------------
DECLARE @data VARCHAR(4000)
SET @data=N'[
{
"Name": "Yoda",
"Id": "001"
},
{
"Name": "Obi-Wan Kenobi",
"Id": "002"
},
{
"Name": "Luke Skywalker",
"Id": "003"
}
]'
SELECT
JSON_QUERY(@data, '$[1]') AS 'JediDataJson',
JSON_VALUE(@data, '$[1].Name') AS 'Name';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment