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, automatically formats the JSON output based on the structure of the source table.

 

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 allows you to have full control over the format of the JSON output. For example, you can specify the root element.

 

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.

 

Watch the Video about JSON Support in SQL Server

 

 

Learn more tips like this!
Enroll to our Online Course!

Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).

Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course

Enroll Now with Discount!

 

Check our Featured Online Courses:

 

Read 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 (3 votes, average: 5.00 out of 5)

Loading...

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

© SQLNetHub