Skip to content

Instantly share code, notes, and snippets.

@samirbehara-zz
Created September 23, 2017 19:17
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 samirbehara-zz/c9e2f8cff066dd2f3acf497626aa7a66 to your computer and use it in GitHub Desktop.
Save samirbehara-zz/c9e2f8cff066dd2f3acf497626aa7a66 to your computer and use it in GitHub Desktop.
-- JSON_VALUE Demonstration to pull one scalar value from the JSON data
DECLARE @json nvarchar(max)
SET @json =N'
{
"Person": [
{
"FirstName": "Gustavo",
"LastName": "Harris",
"AccountNumber": "AW00029484",
"ModifiedDate": "2014-09-12T11:15:07.263"
},
{
"FirstName": "Catherine",
"LastName": "Abel",
"AccountNumber": "AW00029485",
"ModifiedDate": "2014-09-12T11:15:07.263"
}
]
}'
-- Extract scalar value from a JSON text
SELECT JSON_VALUE(@json, '$.Person[0].LastName');
SELECT JSON_VALUE(@json, '$.Person[1].AccountNumber');
-- Returns NULL if the path does not exist
SELECT JSON_VALUE(@json, '$.Person[1].Age');
-- lax option is by default and returns NULL if there is a problem
SELECT JSON_VALUE(@json, 'lax $.Person[1].Age');
-- strict option raises an error if there is a problem
SELECT JSON_VALUE(@json, 'strict $.Person[1].Age');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment