Learn all about Azure SQL Database both Platform as a Service (PaaS) and Infrastructure as a Service (IaaS). Check our online course “Introduction to Azure SQL Database” (lifetime access).
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 allows you to have full control over the format of the JSON output. For example, you can specify the root.
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.
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.
Learn more about Azure SQL Database and get started fast and easy! Through our online course, you will get lifetime access to a comprehensive set of lectures and live demonstrations that will get you started with Azure SQL Database in no time! The course is regularly updated with additional educational material!Enroll Now with Discount!
Check our Featured Online Courses:
- Essential SQL Server Administration Tips
- The Philosophy and Fundamentals of Computer Programming
- Introduction to Azure SQL Database
- SQL Server 2019: What’s New
- Entity Framework: Getting Started (Ultimate Beginners Guide)
- SQL Server Fundamentals (SQL Database for Beginners)
- How to Import and Export Data in SQL Server
- Get Started with SQL Server in 30 Minutes
- 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).