Skip to content

Commit d15177e

Browse files
authored
Freshness pass (#36499)
1 parent ea65497 commit d15177e

4 files changed

Lines changed: 362 additions & 266 deletions

docs/relational-databases/json/format-json-output-automatically-with-auto-mode-sql-server.md

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,56 @@ title: "Format JSON Output Automatically with AUTO Mode"
33
description: "To format the output of the FOR JSON clause automatically based on the structure of the SELECT statement, specify the AUTO option."
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.reviewer: jovanpop, umajay
7-
ms.date: 07/23/2025
6+
ms.reviewer: jovanpop, umajay, randolphwest
7+
ms.date: 01/28/2026
88
ms.service: sql
99
ms.topic: how-to
1010
ms.custom:
1111
- ignite-2025
1212
helpviewer_keywords:
1313
- "FOR JSON AUTO"
14+
ai-usage: ai-assisted
1415
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric || =fabric-sqldb"
1516
---
16-
# Format JSON Output Automatically with AUTO Mode
17+
# Format JSON output automatically with AUTO mode
1718

1819
[!INCLUDE [sqlserver2016-asdb-asdbmi-asa-serverless-pool-only-fabricse-fabricdw-fabricsqldb](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi-asa-svrless-only-fabricse-fabricdw-fabricsqldb.md)]
1920

20-
To format the output of the `FOR JSON` clause automatically based on the structure of the `SELECT` statement, specify the `AUTO` option.
21+
To automatically format the output of the `FOR JSON` clause based on the structure of the `SELECT` statement, specify the `AUTO` option.
2122

2223
When you specify the `AUTO` option, the format of the JSON output is automatically determined based on the order of columns in the SELECT list and their source tables. You can't change this format.
2324

24-
The alternative is to use the `PATH` option to maintain control over the output.
25-
- For more info about the `PATH` option, see [Format Nested JSON Output with PATH Mode](format-nested-json-output-with-path-mode-sql-server.md).
26-
- For an overview of both options, see [Format query results as JSON with FOR JSON](format-query-results-as-json-with-for-json-sql-server.md).
25+
Use the `PATH` option if you want to control the output.
2726

28-
A query that uses the `FOR JSON AUTO` option must have a `FROM` clause.
27+
- For more info about the `PATH` option, see [Format nested JSON output with PATH mode](format-nested-json-output-with-path-mode-sql-server.md).
28+
- For an overview of both options, see [Format query results as JSON with FOR JSON](format-query-results-as-json-with-for-json-sql-server.md).
2929

30-
Here are some examples of the `FOR JSON` clause with the `AUTO` option. [SQL Server Management Studio](/ssms/sql-server-management-studio-ssms) and the [MSSQL extension for Visual Studio Code](../../tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code.md) can auto-format the JSON results (as seen in this article) instead of displaying an unformatted string.
30+
A query that uses the `FOR JSON AUTO` option must have a `FROM` clause.
31+
32+
Here are some examples of the `FOR JSON` clause with the `AUTO` option.
33+
34+
> [!NOTE]
35+
> The [MSSQL extension for Visual Studio Code](../../tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code.md) can auto-format the JSON results (as seen in this article) instead of displaying an unformatted string.
3136
3237
## Examples
3338

34-
### Example 1
39+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
3540

36-
**Query**
41+
### A. Format JSON from a single table
3742

38-
When a query references only one table, the results of the `FOR JSON AUTO` clause are similar to the results of `FOR JSON PATH`. In this case, `FOR JSON AUTO` doesn't create nested objects. The only difference is that `FOR JSON AUTO` outputs dot-separated aliases (for example, `Info.MiddleName` in the following example) as keys with dots, not as nested objects.
43+
When a query references only one table, the results of the `FOR JSON AUTO` clause are similar to the results of `FOR JSON PATH`. In this case, `FOR JSON AUTO` doesn't create nested objects. The only difference is that `FOR JSON AUTO` outputs dot-separated aliases (for example, `Info.MiddleName` in the following example) as keys with dots, not as nested objects.
3944

4045
```sql
41-
SELECT TOP 5
42-
BusinessEntityID As Id,
43-
FirstName, LastName,
44-
Title As 'Info.Title',
45-
MiddleName As 'Info.MiddleName'
46-
FROM Person.Person
47-
FOR JSON AUTO
48-
```
46+
SELECT TOP 5 BusinessEntityID AS Id,
47+
FirstName,
48+
LastName,
49+
Title AS 'Info.Title',
50+
MiddleName AS 'Info.MiddleName'
51+
FROM Person.Person
52+
FOR JSON AUTO;
53+
```
4954

50-
**Result**
55+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
5156

5257
```json
5358
[{
@@ -75,26 +80,24 @@ SELECT TOP 5
7580
"Info.Title": "Ms.",
7681
"Info.MiddleName": "A"
7782
}]
78-
```
83+
```
7984

80-
### Example 2
85+
### B. Format JSON for joined tables
8186

82-
**Query**
83-
84-
When you join tables, columns in the first table are generated as properties of the root object. Columns in the second table are generated as properties of a nested object. The table name or alias of the second table (for example, `D` in the following example) is used as the name of the nested array.
87+
When you join tables, columns in the first table are generated as properties of the root object. Columns in the second table are generated as properties of a nested object. The table name or alias of the second table (for example, `D` in the following example) is used as the name of the nested array.
8588

8689
```sql
87-
SELECT TOP 2 SalesOrderNumber,
88-
OrderDate,
89-
UnitPrice,
90-
OrderQty
91-
FROM Sales.SalesOrderHeader H
92-
INNER JOIN Sales.SalesOrderDetail D
93-
ON H.SalesOrderID = D.SalesOrderID
94-
FOR JSON AUTO
95-
```
96-
97-
**Result**
90+
SELECT TOP 2 SalesOrderNumber,
91+
OrderDate,
92+
UnitPrice,
93+
OrderQty
94+
FROM Sales.SalesOrderHeader AS H
95+
INNER JOIN Sales.SalesOrderDetail AS D
96+
ON H.SalesOrderID = D.SalesOrderID
97+
FOR JSON AUTO;
98+
```
99+
100+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
98101

99102
```json
100103
[{
@@ -113,26 +116,25 @@ FOR JSON AUTO
113116
"OrderQty": 5
114117
}]
115118
}]
116-
```
119+
```
117120

118-
### Example 3
121+
### C. Use FOR JSON PATH to match AUTO output
119122

120-
**Query**
121-
Instead of using FOR JSON AUTO, you can nest a FOR JSON PATH subquery in the SELECT statement, as shown in the following example. This example outputs the same result as the preceding example.
123+
Instead of using `FOR JSON AUTO`, you can nest a `FOR JSON PATH` subquery in the `SELECT` statement, as shown in the following example. This example outputs the same result as the preceding example.
122124

123125
```sql
124-
SELECT TOP 2
125-
SalesOrderNumber,
126-
OrderDate,
127-
(SELECT UnitPrice, OrderQty
128-
FROM Sales.SalesOrderDetail AS D
129-
WHERE H.SalesOrderID = D.SalesOrderID
130-
FOR JSON PATH) AS D
131-
FROM Sales.SalesOrderHeader AS H
132-
FOR JSON PATH
133-
```
134-
135-
**Result**
126+
SELECT TOP 2 SalesOrderNumber,
127+
OrderDate,
128+
(SELECT UnitPrice,
129+
OrderQty
130+
FROM Sales.SalesOrderDetail AS D
131+
WHERE H.SalesOrderID = D.SalesOrderID
132+
FOR JSON PATH) AS D
133+
FROM Sales.SalesOrderHeader AS H
134+
FOR JSON PATH;
135+
```
136+
137+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
136138

137139
```json
138140
[{
@@ -148,13 +150,11 @@ FOR JSON PATH
148150
"UnitPrice": 24.99
149151
}]
150152
}]
151-
```
153+
```
152154

153155
## Learn more about JSON in the SQL Database Engine
154156

155-
For a visual introduction to the built-in JSON support, see the following videos:
156-
157-
- [JSON as a bridge between NoSQL and relational worlds](/events/datadriven-sqlserver2016/json-as-bridge-betwen-nosql-relational-worlds)
157+
For a visual introduction to the built-in JSON support, see [JSON as a bridge between NoSQL and relational worlds](/events/datadriven-sqlserver2016/json-as-bridge-betwen-nosql-relational-worlds).
158158

159159
## Related content
160160

docs/relational-databases/json/format-nested-json-output-with-path-mode-sql-server.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,53 @@ title: "Format Nested JSON Output with PATH Mode"
33
description: "To maintain full control over the output of the FOR JSON clause, specify the PATH option."
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.reviewer: jovanpop, umajay
7-
ms.date: 07/23/2025
6+
ms.reviewer: jovanpop, umajay, randolphwest
7+
ms.date: 01/28/2026
88
ms.service: sql
99
ms.topic: how-to
1010
ms.custom:
1111
- ignite-2025
1212
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric || =fabric-sqldb"
1313
---
14-
# Format Nested JSON Output with PATH Mode
14+
# Format nested JSON output with PATH mode
1515

1616
[!INCLUDE [sqlserver2016-asdb-asdbmi-asa-serverless-pool-only-fabricse-fabricdw-fabricsqldb](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi-asa-svrless-only-fabricse-fabricdw-fabricsqldb.md)]
1717

1818
To maintain full control over the output of the `FOR JSON` clause, specify the `PATH` option.
1919

20-
`PATH` mode lets you create wrapper objects and nest complex properties. The results are formatted as an array of JSON objects.
20+
`PATH` mode lets you create wrapper objects and nest complex properties. The results are formatted as an array of JSON objects.
2121

2222
The alternative is to use the `AUTO` option to format the output automatically based on the structure of the `SELECT` statement.
23-
- For more info about the `AUTO` option, see [Format JSON Output Automatically with AUTO Mode](format-json-output-automatically-with-auto-mode-sql-server.md) .
24-
- For an overview of both options, see [Format query results as JSON with FOR JSON](format-query-results-as-json-with-for-json-sql-server.md).
2523

26-
Here are some examples of the `FOR JSON` clause with the `PATH` option. Format nested results by using dot-separated column names or by using nested queries, as shown in the following examples. By default, null values are not included in `FOR JSON` output. [SQL Server Management Studio](/ssms/sql-server-management-studio-ssms) and the [MSSQL extension for Visual Studio Code](../../tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code.md) can auto-format the JSON results (as seen in this article) instead of displaying an unformatted string.
24+
- For more info about the `AUTO` option, see [Format JSON output automatically with AUTO mode](format-json-output-automatically-with-auto-mode-sql-server.md).
25+
- For an overview of both options, see [Format query results as JSON with FOR JSON](format-query-results-as-json-with-for-json-sql-server.md).
2726

28-
## Example - Dot-separated column names
27+
The following examples show how to use the `FOR JSON` clause with the `PATH` option. Format nested results by using dot-separated column names or by using nested queries, as shown in the examples. By default, null values aren't included in `FOR JSON` output.
2928

30-
The following query formats the first five rows from the AdventureWorks `Person` table as JSON.
29+
> [!NOTE]
30+
> The [MSSQL extension for Visual Studio Code](../../tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code.md) can auto-format the JSON results (as seen in this article) instead of displaying an unformatted string.
3131
32-
The `FOR JSON PATH` clause uses the column alias or column name to determine the key name in the JSON output. If an alias contains dots, the `PATH` option creates nested objects.
32+
## Examples
3333

34-
**Query**
34+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
35+
36+
### A. Dot-separated column names
37+
38+
The following query formats the first five rows from the AdventureWorks `Person` table as JSON.
39+
40+
The `FOR JSON PATH` clause uses the column alias or column name to determine the key name in the JSON output. If an alias contains dots, the `PATH` option creates nested objects.
3541

3642
```sql
37-
SELECT TOP 5
38-
BusinessEntityID As Id,
39-
FirstName, LastName,
40-
Title As 'Info.Title',
41-
MiddleName As 'Info.MiddleName'
42-
FROM Person.Person
43-
FOR JSON PATH
44-
```
43+
SELECT TOP 5 BusinessEntityID AS Id,
44+
FirstName,
45+
LastName,
46+
Title AS 'Info.Title',
47+
MiddleName AS 'Info.MiddleName'
48+
FROM Person.Person
49+
FOR JSON PATH;
50+
```
4551

46-
**Result**
52+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
4753

4854
```json
4955
[{
@@ -77,26 +83,24 @@ SELECT TOP 5
7783
"MiddleName": "A"
7884
}
7985
}]
80-
```
86+
```
8187

82-
## Example - Multiple tables
88+
### B. Multiple tables
8389

84-
If you reference more than one table in a query, `FOR JSON PATH` nests each column using its alias. The following query creates one JSON object per (`OrderHeader, OrderDetails`) pair joined in the query.
85-
86-
**Query**
90+
If you reference more than one table in a query, `FOR JSON PATH` nests each column using its alias. The following query creates one JSON object for each `(OrderHeader, OrderDetails)` pair that the query joins.
8791

8892
```sql
89-
SELECT TOP 2 H.SalesOrderNumber AS 'Order.Number',
90-
H.OrderDate AS 'Order.Date',
91-
D.UnitPrice AS 'Product.Price',
92-
D.OrderQty AS 'Product.Quantity'
93-
FROM Sales.SalesOrderHeader H
94-
INNER JOIN Sales.SalesOrderDetail D
95-
ON H.SalesOrderID = D.SalesOrderID
96-
FOR JSON PATH
97-
```
98-
99-
**Result**
93+
SELECT TOP 2 H.SalesOrderNumber AS 'Order.Number',
94+
H.OrderDate AS 'Order.Date',
95+
D.UnitPrice AS 'Product.Price',
96+
D.OrderQty AS 'Product.Quantity'
97+
FROM Sales.SalesOrderHeader AS H
98+
INNER JOIN Sales.SalesOrderDetail AS D
99+
ON H.SalesOrderID = D.SalesOrderID
100+
FOR JSON PATH;
101+
```
102+
103+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
100104

101105
```json
102106
[{
@@ -116,13 +120,11 @@ FOR JSON PATH
116120
"Price": 2024.9940
117121
}
118122
}]
119-
```
123+
```
120124

121125
## Learn more about JSON in the SQL Database Engine
122126

123-
For a visual introduction to the built-in JSON support, see the following videos:
124-
125-
- [JSON as a bridge between NoSQL and relational worlds](/events/datadriven-sqlserver2016/json-as-bridge-betwen-nosql-relational-worlds)
127+
For a visual introduction to the built-in JSON support, see [JSON as a bridge between NoSQL and relational worlds](/events/datadriven-sqlserver2016/json-as-bridge-betwen-nosql-relational-worlds).
126128

127129
## Related content
128130

0 commit comments

Comments
 (0)