Formatting a JSON output as a single object

The default JSON output is enclosed within square brackets, which means the output is an array. If you want to format it as a single object instead of an array, use the WITHOUT_ARRAY_WRAPPER option.

Even if a query returns only one row, SQL Server will format it by default as a JSON array, as in the following example:

SELECT PersonID, FullName, EmailAddress, PhoneNumber  
FROM Application.People WHERE PersonID = 2 FOR JSON AUTO; 

Although only one row is returned, the output is still an array (with a single element):

[
  {
    "PersonID":2,
    "FullName":"Kayla Woodcock",
    "EmailAddress":"kaylaw@wideworldimporters.com",
    "PhoneNumber":"(415) 555-0102"
  }
]

To return a single object instead of an array, you can ...

Get SQL Server 2017 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.