Sources are heterogeneous and SQL Server allows various
types of manipulations and one of the interesting feature in SQL Server 2016 is Querying
data of JSON Format. There are four basic syntax which we can use against a JSON
data type.
We can demonstrate the
use of these syntax with a simple scenario, first off I will define a simple JSON
array and walk through each syntax one by one.
Use of ISJSON()
ISJSON() syntax is much similar to the ISNUMERIC() where the latter one checks for a numeric data type and outputs one if it’s true like wise ISJSON() out puts one if the evaluated value is in valid JSON Format.
Try This DEMO
DECLARE @jsonInfo NVARCHAR(MAX)
SET @jsonInfo=N'{
"info":{
"type":1,
"address":{
"town":"Colombo",
"country":"SriLanka"
},
"tags":["Sport", "Water polo"]
},
"type":"Basic"
}'
SELECT ISJSON(@jsonInfo)
Use of JSON_VALUE VS JSON_QUERY
Json value syntax looks for a specific
value in the JSON array, here the path will start from a $ sign and the
respective path root has to be configured, if there are multiple values or a sub
array at the end of the path then JSON value will return NULL, it strictly checks for a
single value where JSONQUERY() looks for an array and if it finds a single value at the end of the path then it will return NULL.Use of JSON_VALUE VS JSON_QUERY
DECLARE @jsonInfo NVARCHAR(MAX)
SET @jsonInfo=N'{
"info":{
"type":1,
"address":{
"town":"Colombo",
"country":"SriLanka"
},
"tags":["Sport",
"Water polo"]
},
"type":"Basic"
}'
select
JSON_VALUE(@jsonInfo,'$.info.address.town') AS [Single value as JSON VALUE]
select
JSON_VALUE (@jsonInfo,'$.info') AS [Array as JSON VALUE]
select JSON_QUERY(@jsonInfo,'$.info.address.town') AS [Single value as JSON QUERY]
select JSON_QUERY(@jsonInfo,'$.info.address.town') AS [Single value as JSON QUERY]
select
JSON_QUERY(@jsonInfo,'$.info') AS [Array as JSON QUERY]
Use of JSON_MODIFY()
Json modify allows you to modify a
value in the end of an array path, so you can set a different value in an array, you can
try the following demo
DECLARE @jsonInfo NVARCHAR(MAX)
SET @jsonInfo=N'{
"info":{
"type":1,
"address":{
"town":"Colombo",
"country":"SriLanka"
},
"tags":["Sport",
"Water polo"]
},
"type":"Basic"
}'
select JSON_VALUE(@jsonInfo,'$.info.address.town')
AS [Past Town]
set @jsonInfo= JSON_MODIFY(@jsonInfo,'$.info.address.town','Jaffna')
select JSON_VALUE(@jsonInfo,'$.info.address.town')
AS [Current Town]
In the above scenario I'm setting a different value at the end of the array path and when I select it afterwards it gives the updated value as output.
In the above scenario I'm setting a different value at the end of the array path and when I select it afterwards it gives the updated value as output.
In a holistic view data manipulation is becoming more and more simple with such new additions, simply now if we have a data source in the form of JSON data type it can be easily queried from SSMS itself but again your version has to be 2016 or latter.
ReplyDeleteI like your blog, I read this blog please update more content on hacking,Nice post
tableau online training