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:
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.
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.
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.
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!
Check our Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals (SQL Database for Beginners)
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners: Windows Forms (C#)
- Introduction to Data Science and SQL Server Machine Learning
- Introduction to Azure SQL Database
- SQL Server 2019: What’s New
- Entity Framework: Getting Started (Ultimate Beginners Guide)
- How to Import and Export Data in SQL Server
- Get Started with SQL Server in 30 Minutes
- A Guide on How to Start and Monetize a Successful Blog
- MSDN Library Article: What’s New in SQL Server 2016, November Update
- MSDN Library Article: Format Query Results as JSON with FOR JSON (SQL Server)
- MSDN Library Article: Convert JSON Data to Rows and Columns with OPENJSON (SQL Server)
- SQL Server 2016 Top Features
- SQL Server 2016 SP1: Breakthrough Changes
- Temporal Tables in SQL Server and Azure SQL Database
- SQL Server 2016: Built-In JSON Support
- Azure Cosmos DB: Learn by Example
- Learn Azure SQL Database: Creating your First Database
- How to Create an Azure SQL Server Virtual Machine
- How to Backup a SQL Server Database from On-Premises to Azure Storage
- What is Azure Advisor?
- … all our Azure-related articles
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:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
Artemakis Artemiou is a Senior SQL Server 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).