In this article, we will discussing about built-in JSON support in SQL Server and Azure SQL Database.
What is JSON?
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:
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.
Learn more about Azure SQL Database: Enroll to our Online Course!
Enroll to our online course on Udemy titled “Introduction to Azure SQL Database for Beginners” and get lifetime access to high-quality lessons and hands-on guides about all aspects of Azure SQL Database.
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
Strengthen you SQL Server Development Skills – 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).Via the course, you will 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
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Read More:
- 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)
Check Also
- 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)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.