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
{{ message }}
Repository navigation
Commit 90bf03e
Browse filesBrowse the repository at this point in the historyBrowse files
Format query results as JSON, or export data from SQL Server as JSON, by adding the `FOR JSON` clause to a `SELECT` statement. Use the `FOR JSON` clause to simplify client applications by delegating the formatting of JSON output from the app to [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
23
24
24
-
> [!NOTE]
25
+
> [!NOTE]
25
26
> [Azure Data Studio](../../azure-data-studio/download-azure-data-studio.md) is the recommended query editor for JSON queries because it auto-formats the JSON results (as seen in this article) instead of displaying a flat string.
26
-
27
-
When you use the `FOR JSON` clause, you can specify the structure of the JSON output explicitly, or let the structure of the SELECT statement determine the output.
28
-
29
-
-To maintain full control over the format of the JSON output, use `FOR JSON PATH`. You can create wrapper objects and nest complex properties.
30
-
31
-
-To format the JSON output automatically based on the structure of the SELECT statement, use `FOR JSON AUTO`.
32
-
27
+
28
+
When you use the `FOR JSON` clause, you can specify the structure of the JSON output explicitly, or let the structure of the SELECT statement determine the output.
29
+
30
+
- To maintain full control over the format of the JSON output, use `FOR JSON PATH`. You can create wrapper objects and nest complex properties.
31
+
32
+
- To format the JSON output automatically based on the structure of the SELECT statement, use `FOR JSON AUTO`.
33
+
33
34
Here's an example of a **SELECT** statement with the `FOR JSON` clause and its output.
:::image type="content" source="media/format-query-results-as-json-with-for-json-sql-server/for-json.png" alt-text="Diagram showing how FOR JSON works." lightbox="media/format-query-results-as-json-with-for-json-sql-server/for-json.png":::
37
+
38
38
## Option 1 - You control output with FOR JSON PATH
39
-
In **PATH** mode, you can use the dot syntax - for example, `'Item.UnitPrice'` - to format nested output.
40
39
41
-
Here's a sample query that uses **PATH** mode with the `FOR JSON` clause. The following example also uses the **ROOT** option to specify a named root element.
42
-
43
-
:::image type="content" source="../../relational-databases/json/media/forjson-example1.png" alt-text="Diagram of flow of FOR JSON output" lightbox="../../relational-databases/json/media/forjson-example1.png":::
40
+
In **PATH** mode, you can use the dot syntax - for example, `'Item.Price'` - to format nested output.
41
+
42
+
Here's a sample query that uses **PATH** mode with the `FOR JSON` clause. The following example also uses the **ROOT** option to specify a named root element.
43
+
44
+
:::image type="content" source="media/format-query-results-as-json-with-for-json-sql-server/for-json-path.png" alt-text="Diagram of flow of FOR JSON output." lightbox="media/format-query-results-as-json-with-for-json-sql-server/for-json-path.png":::
44
45
45
46
### More info about FOR JSON PATH
47
+
46
48
For more detailed info and examples, see [Format Nested JSON Output with PATH Mode (SQL Server)](../../relational-databases/json/format-nested-json-output-with-path-mode-sql-server.md).
47
49
48
-
For syntax and usage, see [FOR Clause (Transact-SQL)](../../t-sql/queries/select-for-clause-transact-sql.md).
50
+
For syntax and usage, see [FOR Clause (Transact-SQL)](../../t-sql/queries/select-for-clause-transact-sql.md).
49
51
50
52
## Option 2 - SELECT statement controls output with FOR JSON AUTO
53
+
51
54
In **AUTO** mode, the structure of the SELECT statement determines the format of the JSON output.
52
55
53
-
By default, **null** values are not included in the output. You can use `INCLUDE_NULL_VALUES` to change this behavior.
56
+
By default, **null** values aren't included in the output. You can use `INCLUDE_NULL_VALUES` to change this behavior.
54
57
55
58
Here's a sample query that uses **AUTO** mode with the `FOR JSON` clause.
56
59
57
-
```sql
58
-
SELECT name, surname
59
-
FROM emp
60
+
```sql
61
+
SELECT name, surname
62
+
FROM emp
60
63
FOR JSON AUTO;
61
-
```
64
+
```
62
65
63
66
And here is the returned JSON.
64
67
65
-
```json
68
+
```json
66
69
[{
67
70
"name": "John"
68
71
}, {
@@ -73,71 +76,56 @@ And here is the returned JSON.
73
76
74
77
### 2.b - Example with JOIN and NULL
75
78
76
-
The following example of `SELECT...FOR JSON AUTO` includes a display of what the JSON results look like when there is a 1:Many relationship between data from `JOIN`'ed tables.
79
+
The following example of `SELECT...FOR JSON AUTO` includes a display of what the JSON results look like when there is a *1:many* relationship between data from joined tables.
77
80
78
81
The absence of the null value from the returned JSON is also illustrated. However, you can override this default behavior by use of the `INCLUDE_NULL_VALUES` keyword on the `FOR` clause.
79
82
80
83
```sql
81
-
go
82
-
83
84
DROPTABLE IF EXISTS #tabStudent;
84
85
DROPTABLE IF EXISTS #tabClass;
86
+
GO
85
87
86
-
go
87
-
88
-
CREATE TABLE #tabClass
89
-
(
90
-
ClassGuid uniqueIdentifier not null default newid(),
91
-
ClassName nvarchar(32) not null
88
+
CREATE TABLE #tabClass (
89
+
ClassGuid UNIQUEIDENTIFIER NOT NULL DEFAULT newid(),
90
+
ClassName NVARCHAR(32) NOT NULL
92
91
);
93
92
94
-
CREATE TABLE #tabStudent
95
-
(
96
-
StudentGuid uniqueIdentifier not null default newid(),
97
-
StudentName nvarchar(32) not null,
98
-
ClassGuid uniqueIdentifier null-- Foreign key.
93
+
CREATE TABLE #tabStudent (
94
+
StudentGuid UNIQUEIDENTIFIER NOT NULL DEFAULT newid(),
For more detailed info and examples, see [Format JSON Output Automatically with AUTO Mode (SQL Server)](../../relational-databases/json/format-json-output-automatically-with-auto-mode-sql-server.md).
160
148
161
-
For syntax and usage, see [FOR Clause (Transact-SQL)](../../t-sql/queries/select-for-clause-transact-sql.md).
162
-
163
-
## Control other JSON output options
164
-
Control the output of the `FOR JSON` clause by using the following additional options.
165
-
166
-
-**ROOT**. To add a single, top-level element to the JSON output, specify the **ROOT** option. If you don't specify this option, the JSON output doesn't have a root element. For more info, see [Add a Root Node to JSON Output with the ROOT Option (SQL Server)](../../relational-databases/json/add-a-root-node-to-json-output-with-the-root-option-sql-server.md).
167
-
168
-
-**INCLUDE_NULL_VALUES**. To include null values in the JSON output, specify the **INCLUDE_NULL_VALUES** option. If you don't specify this option, the output doesn't include JSON properties for NULL values in the query results. For more info, see [Include Null Values in JSON Output with the INCLUDE_NULL_VALUES Option (SQL Server)](../../relational-databases/json/include-null-values-in-json-include-null-values-option.md).
169
-
170
-
-**WITHOUT_ARRAY_WRAPPER**. To remove the square brackets that surround the JSON output of the `FOR JSON` clause by default, specify the **WITHOUT_ARRAY_WRAPPER** option. Use this option to generate a single JSON object as output from a single-row result. If you don't specify this option, the JSON output is formatted as an array - that is, it's enclosed within square brackets. For more info, see [Remove Square Brackets from JSON Output with the WITHOUT_ARRAY_WRAPPER Option (SQL Server)](../../relational-databases/json/remove-square-brackets-from-json-without-array-wrapper-option.md).
171
-
172
-
## Output of the FOR JSON clause
173
-
The output of the `FOR JSON` clause has the following characteristics:
174
-
175
-
1. The result set contains a single column.
176
-
- A small result set may contain a single row.
177
-
- A large result set splits the long JSON string across multiple rows.
178
-
- By default, SQL Server Management Studio (SSMS) concatenates the results into a single row when the output setting is **Results to Grid**. The SSMS status bar displays the actual row count.
179
-
- Other client applications may require code to recombine lengthy results into a single, valid JSON string by concatenating the contents of multiple rows. For an example of this code in a C# application, see [Use FOR JSON output in a C# client app](../../relational-databases/json/use-for-json-output-in-sql-server-and-in-client-apps-sql-server.md#use-for-json-output-in-a-c-client-app).
180
-
181
-
:::image type="content" source="../../relational-databases/json/media/forjson-example2.png" alt-text="Example of FOR JSON output" lightbox="../../relational-databases/json/media/forjson-example2.png":::
182
-
183
-
184
-
2. The results are formatted as an array of JSON objects.
185
-
186
-
- The number of elements in the JSON array is equal to the number of rows in the results of the SELECT statement (before the FOR JSON clause is applied).
187
-
188
-
- Each row in the results of the SELECT statement (before the FOR JSON clause is applied) becomes a separate JSON object in the array.
189
-
190
-
- Each column in the results of the SELECT statement (before the FOR JSON clause is applied) becomes a property of the JSON object.
191
-
192
-
3. Both the names of columns and their values are escaped according to JSON syntax. For more info, see [How FOR JSON escapes special characters and control characters (SQL Server)](../../relational-databases/json/how-for-json-escapes-special-characters-and-control-characters-sql-server.md).
149
+
For syntax and usage, see [FOR Clause (Transact-SQL)](../../t-sql/queries/select-for-clause-transact-sql.md).
150
+
151
+
## Control other JSON output options
152
+
153
+
Control the output of the `FOR JSON` clause by using the following additional options.
154
+
155
+
-**ROOT**. To add a single, top-level element to the JSON output, specify the **ROOT** option. If you don't specify this option, the JSON output doesn't have a root element. For more info, see [Add a Root Node to JSON Output with the ROOT Option (SQL Server)](../../relational-databases/json/add-a-root-node-to-json-output-with-the-root-option-sql-server.md).
156
+
157
+
-**INCLUDE_NULL_VALUES**. To include null values in the JSON output, specify the **INCLUDE_NULL_VALUES** option. If you don't specify this option, the output doesn't include JSON properties for NULL values in the query results. For more info, see [Include Null Values in JSON Output with the INCLUDE_NULL_VALUES Option (SQL Server)](../../relational-databases/json/include-null-values-in-json-include-null-values-option.md).
158
+
159
+
-**WITHOUT_ARRAY_WRAPPER**. To remove the square brackets that surround the JSON output of the `FOR JSON` clause by default, specify the **WITHOUT_ARRAY_WRAPPER** option. Use this option to generate a single JSON object as output from a single-row result. If you don't specify this option, the JSON output is formatted as an array - that is, it's enclosed within square brackets. For more info, see [Remove Square Brackets from JSON Output with the WITHOUT_ARRAY_WRAPPER Option (SQL Server)](../../relational-databases/json/remove-square-brackets-from-json-without-array-wrapper-option.md).
160
+
161
+
## Output of the FOR JSON clause
162
+
163
+
The output of the `FOR JSON` clause has the following characteristics:
164
+
165
+
1. The result set contains a single column.
166
+
- A small result set may contain a single row.
167
+
- A large result set splits the long JSON string across multiple rows.
168
+
- By default, SQL Server Management Studio (SSMS) concatenates the results into a single row when the output setting is **Results to Grid**. The SSMS status bar displays the actual row count.
169
+
- Other client applications may require code to recombine lengthy results into a single, valid JSON string by concatenating the contents of multiple rows. For an example of this code in a C# application, see [Use FOR JSON output in a C# client app](../../relational-databases/json/use-for-json-output-in-sql-server-and-in-client-apps-sql-server.md#use-for-json-output-in-a-c-client-app).
170
+
171
+
:::image type="content" source="media/format-query-results-as-json-with-for-json-sql-server/for-json-output.png" alt-text="Screenshot of FOR JSON output in SQL Server Management Studio." lightbox="media/format-query-results-as-json-with-for-json-sql-server/for-json-output.png":::
172
+
173
+
1. The results are formatted as an array of JSON objects.
174
+
175
+
- The number of elements in the JSON array is equal to the number of rows in the results of the SELECT statement (before the FOR JSON clause is applied).
176
+
177
+
- Each row in the results of the SELECT statement (before the FOR JSON clause is applied) becomes a separate JSON object in the array.
178
+
179
+
- Each column in the results of the SELECT statement (before the FOR JSON clause is applied) becomes a property of the JSON object.
180
+
181
+
1. Both the names of columns and their values are escaped according to JSON syntax. For more info, see [How FOR JSON escapes special characters and control characters (SQL Server)](../../relational-databases/json/how-for-json-escapes-special-characters-and-control-characters-sql-server.md).
193
182
194
183
### Example
195
-
Here's an example that demonstrates how the `FOR JSON` clause formats the JSON output.
196
-
197
-
**Query results**
184
+
185
+
Here's an example that demonstrates how the `FOR JSON` clause formats the JSON output.
186
+
187
+
#### Query results
198
188
199
189
|A|B|C|D|
200
190
|-|-|-|-|
201
191
|10|11|12|X|
202
192
|20|21|22|Y|
203
-
|30|31|32|Z|
204
-
| | | | |
193
+
|30|31|32|Z|
194
+
195
+
#### JSON output
205
196
206
-
**JSON output**
207
-
208
-
```json
197
+
```json
209
198
[{
210
199
"A": 10,
211
200
"B": 11,
@@ -221,28 +210,16 @@ Here's an example that demonstrates how the `FOR JSON` clause formats the JSON o
221
210
"B": 31,
222
211
"C": 32,
223
212
"D": "Z"
224
-
}]
225
-
```
226
-
227
-
For more info about what you see in the output of the `FOR JSON` clause, see the following articles:
228
-
229
-
-[How FOR JSON converts SQL Server data types to JSON data types (SQL Server)](../../relational-databases/json/how-for-json-converts-sql-server-data-types-to-json-data-types-sql-server.md)
230
-
The `FOR JSON` clause uses the rules described in this article to convert SQL data types to JSON types in the JSON output.
231
-
232
-
-[How FOR JSON escapes special characters and control characters (SQL Server)](../../relational-databases/json/how-for-json-escapes-special-characters-and-control-characters-sql-server.md)
233
-
The `FOR JSON` clause escapes special characters and represents control characters in the JSON output as described in this article.
213
+
}]
214
+
```
234
215
235
-
## Learn more about JSON in SQL Server and Azure SQL Database
236
-
237
-
### Microsoft videos
216
+
## See also
238
217
239
-
For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:
218
+
-[How FOR JSON converts SQL Server data types to JSON data types (SQL Server)](../../relational-databases/json/how-for-json-converts-sql-server-data-types-to-json-data-types-sql-server.md)
219
+
-[How FOR JSON escapes special characters and control characters (SQL Server)](../../relational-databases/json/how-for-json-escapes-special-characters-and-control-characters-sql-server.md)
220
+
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven-SQLServer2016/JSON-as-bridge-betwen-NoSQL-relational-worlds)
240
221
241
-
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven-SQLServer2016/JSON-as-bridge-betwen-NoSQL-relational-worlds)
-[Use FOR JSON output in SQL Server and in client apps (SQL Server)](../../relational-databases/json/use-for-json-output-in-sql-server-and-in-client-apps-sql-server.md)
-[Use FOR JSON output in SQL Server and in client apps (SQL Server)](../../relational-databases/json/use-for-json-output-in-sql-server-and-in-client-apps-sql-server.md)
0 commit comments