Last active
February 6, 2017 22:09
-
-
Save bertwagner/04f4d447254b87f250d9b22ffa8d1d13 to your computer and use it in GitHub Desktop.
SQL Server 2016 JSON's JSON_QUERY() Function Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- See https://gist.github.com/bertwagner/356bf47732b9e35d2156daa943e049e9 for a formatted version of this JSON | |
DECLARE @garage nvarchar(1000) = N'{ "Cars": [{ "Make": "Volkswagen", "Model": { "Base": "Golf", "Trim": "GL" }, "Year": 2003, "PurchaseDate": "2006-10-05T00:00:00.000Z" }, { "Make": "Subaru", "Model": { "Base": "Impreza", "Trim": "Premium" }, "Year": 2016, "PurchaseDate": "2015-08-18T00:00:00.000Z" }] }' | |
-- We use JSON_QUERY to get the JSON representation of the Cars array | |
SELECT JSON_QUERY(@garage, '$.Cars') | |
-- Output: [{ "Make": "Volkswagen", "Model": { "Base": "Golf", "Trim": "GL" }, "Year": 2003, "PurchaseDate": "2006-10-05T00:00:00.000Z" }, { "Make": "Subaru", "Model": { "Base": "Impreza", "Trim": "Premium" }, "Year": 2016, "PurchaseDate": "2015-08-18T00:00:00.000Z" }] | |
-- If we combine it with JSON_VALUE we can then pull out specific scalar values | |
SELECT JSON_VALUE(JSON_QUERY(@garage, '$.Cars') , '$[0].Make') | |
-- Output: Volkswagen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment