You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
21
22
22
23
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.
23
24
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.
27
26
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).
29
29
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.
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.
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.
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.
## Learn more about JSON in the SQL Database Engine
154
156
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).
To maintain full control over the output of the `FOR JSON` clause, specify the `PATH` option.
19
19
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.
21
21
22
22
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).
25
23
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).
27
26
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.
29
28
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.
31
31
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.
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.
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.
## Learn more about JSON in the SQL Database Engine
122
126
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).
0 commit comments