Built-In JSON Support in SQL Server and Azure SQL Database

Built-In JSON Support in SQL Server and Azure SQL Database

Built-In JSON Support in SQL Server and Azure SQL Database

JSON stands for JavaScript Object Notation and it is widely used for data representation in the data services world. With built-In JSON support in SQL Server and Azure SQL Database, you can format query results as JSON by adding the “FOR JSON” clause to a SELECT statement in SQL Server and Azure SQL Database. Also, you can convert JSON data to rows and columns by calling the OPENJSON rowset provider function.

 

Examples of Built-In JSON Support in SQL Server and Azure SQL Database

Below we are going to see some examples of utilizing SQL Server’s JSON support.

For the purposes of these demos, I have created a sample tabled called “Students” and I have populated it with random data. Let’s take a look at the first 10 rows of this table:

 

Built-In JSON Support in SQL Server and Azure SQL Database (1) - SQLNetHub

Figure 1: Sample Table “Students”.

 

Example 1: The “FOR JSON AUTO” Clause

In order to extract data from a table and represent it in the JSON format you must use wither the “FOR JSON AUTO” clause or the “FOR JSON PATH” clause.

The “FOR JSON AUTO” clause allows you to have full control over the format of the JSON output. For example, you can specify the root.

 

Built-In JSON Support in SQL Server and Azure SQL Database (2) - SQLNetHub

Figure 2: Using the “FOR JSON AUTO” Clause.

 

Example 2: The “FOR JSON PATH” clause


The “FOR JSON PATH” clause automatically formats the JSON output based on the structure of the source table.

 

Built-In JSON Support in SQL Server and Azure SQL Database (3) - SQLNetHub

Figure 3: Using the “FOR JSON PATH” Clause.

 

Example 3: The OPENJSON rowset function

Now, if you want to transform JSON data into a tabular form, you can use the OPENJSON rowset function.

 

Built-In JSON Support in SQL Server and Azure SQL Database (4) - SQLNetHub

Figure 4: Using the “OPENJSON” Rowset Function.

 

Learn More: 

 

Check Also

 

Check our latest software releases!

Easily generate SQL code snippets with Snippets Generator!

Convert static T-SQL to dynamic and vice versa with Dynamic SQL Generator.

Secure your SQL Server instances with DBA Security Advisor.

Benchmark SQL Server memory-optimized tables with In-Memory OLTP Simulator.

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub

 

Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server and Software Architect, Author, and a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Artemakis's official website can be found at aartemiou.com.