05
Jan
JSON in SQL Server 2016: Part 1 of 4
JSON is currently one of the most commonly used data exchange formats. Most modern services return information formatted as JSON text; web browsers use information formatted as JSON. JSON is the common format when semi-structured information needs to be stored, such as in files. Because a lot of data is formatted as JSON, it is important to enable SQL Server to process JSON text retrieved from other systems or to format information retrieved from SQL Server tables as JSON text.
As the most-requested feature on the Microsoft SQL Server connect site, with more than 1,000 votes, support for JSON text processing has been added to SQL Server 2016. In this post, we will present a high-level overview of JSON functionality in SQL Server 2016.
Overview
JSON functions in SQL Server enable you to analyze and query JSON data, transform JSON to relational format, and export SQL query results as JSON text.
If you have JSON text, you can extract data from JSON or verify that JSON is properly formatted using built-in functions JSON_VALUE, JSON_QUERY, and ISJSON. For more advanced querying and analysis, the OPENJSON function can transform an array of JSON objects into a set of rows. Any SQL query can be executed on the returned result set. Finally, there is the FOR JSON clause that enables you to format query results as JSON text.
We can start with simple examples. In the following Transact-SQL code, we will define a text variable where we will put JSON text:
DECLARE @json NVARCHAR(4000)
SET @json =
N'{
“info”:{
“type”:1,
“address”:{
“town”:”Bristol”,
“county”:”Avon”,
“country”:”England”
},
“tags”:[“Sport”, “Water polo”]
},
“type”:”Basic”
}’
Now, we can extract values and objects from JSON text using the JSON_VALUE and JSON_QUERY functions:
SELECT
JSON_VALUE(@json, ‘$.type’) as type,
JSON_VALUE(@json, ‘$.info.address.town’) as town,
JSON_QUERY(@json, ‘$.info.tags’) as tags
This query will return “Basic”, “Bristol”, and [“Sport”, “Water polo”] values. The JSON_VALUE function returns one scalar value from JSON text (e.g. strings, numbers, true/false) that is placed on a JSON path specified as the second parameter. JSON_QUERY returns an object or array (in this example an array of tags) on the JSON path. JSON built-in functions use JavaScript-like syntax to reference values and objects in JSON text via second parameter.
The OPENJSON function enables you to reference some array in JSON text and return elements from that array:
SELECT value
FROM OPENJSON(@json, ‘$.info.tags’)
In this example, string values from the tags array are returned. However, the OPENJSON function can return any complex object.
Finally, there is a FOR JSON clause that can format any result set returned by SQL query as JSON text:
SELECT object_id, name
FROM sys.tables
FOR JSON PATH
Check out the other posts in this four-part series in the links below (as they become available), or learn more in the SQL Server 2016 blogging series.
JSON in SQL Server 2016: Part 2 of 4
JSON in SQL Server 2016: Part 3 of 4
JSON in SQL Server 2016: Part 4 of 4