Retrieving SQL Server data in the JSON format

This section explores JSON support in SQL Server with a very common action: formatting tabular data as JSON. In SQL Server 2016, the clause FOR JSON can be used with the SELECT statement to accomplish this. It is analogous to formatting relational data as XML by using the FOR XML extension.

When you use the FOR JSON clause, you can choose between two modes:

  • FOR JSON AUTO: The JSON output will be formatted by the structure of the SELECT statement automatically.
  • FOR JSON PATH: The JSON output will be formatted by the structure explicitly defined by you. With JSON PATH, you can create a more complex output (nested objects and properties).

In both modes, SQL Server extracts relational data defined by the ...

Get SQL Server 2016 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.