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 add a single, top-level element to the JSON output of the **FOR JSON** clause, specify the **ROOT** option.
19
-
20
-
If you don't specify the **ROOT** option, the JSON output doesn't include a root element.
21
-
22
-
## Examples
23
-
The following table shows the output of the **FOR JSON** clause with and without the **ROOT** option.
24
-
25
-
The examples in the following table assume that the optional *RootName* argument is empty. If you provide a name for the root element, this value replaces the value **root** in the examples.
26
-
27
-
Without the **ROOT** option
28
-
29
-
```json
14
+
# Add a Root Node to JSON Output with the ROOT Option
To add a single, top-level element to the JSON output of the `FOR JSON` clause, specify the `ROOT` option.
19
+
20
+
If you don't specify the `ROOT` option, the JSON output doesn't include a root element.
21
+
22
+
## Examples
23
+
24
+
The following table shows the output of the `FOR JSON` clause with and without the `ROOT` option.
25
+
26
+
The examples in the following table assume that the optional *RootName* argument is empty. If you provide a name for the root element, this value replaces the value `root` in the examples.
27
+
28
+
**Without the `ROOT` option:**
29
+
30
+
```json
30
31
{
31
32
<<json properties>>
32
33
}
33
34
```
34
-
35
-
```json
35
+
36
+
```json
36
37
[
37
38
<<json array elements>>
38
39
]
39
40
```
40
-
41
-
With the **ROOT** option
42
-
43
-
```json
41
+
42
+
**With the `ROOT` option:**
43
+
44
+
```json
44
45
{
45
46
"root": {
46
47
<<json properties>>
47
48
}
48
49
}
49
50
```
50
-
51
-
```json
51
+
52
+
```json
52
53
{
53
54
"root": [
54
55
<< json array elements >>
55
56
]
56
57
}
57
58
```
58
-
59
-
Here's another example of a **FOR JSON** clause with the **ROOT** option. This example specifies a value for the optional *RootName* argument.
60
-
59
+
60
+
Here's another example of a `FOR JSON` clause with the `ROOT` option. This example specifies a value for the optional `RootName` argument.
61
+
61
62
**Query**
62
-
63
-
```sql
63
+
64
+
```sql
64
65
SELECT TOP 5
65
66
BusinessEntityID As Id,
66
67
FirstName, LastName,
@@ -69,10 +70,10 @@ SELECT TOP 5
69
70
FROMPerson.Person
70
71
FOR JSON PATH, ROOT('info')
71
72
```
72
-
73
+
73
74
**Result**
74
-
75
-
```json
75
+
76
+
```json
76
77
{
77
78
"info": [{
78
79
"Id": 1,
@@ -107,10 +108,10 @@ SELECT TOP 5
107
108
}]
108
109
}
109
110
```
110
-
111
+
111
112
**Result (without root)**
112
-
113
-
```json
113
+
114
+
```json
114
115
[{
115
116
"Id": 1,
116
117
"FirstName": "Ken",
@@ -144,18 +145,12 @@ SELECT TOP 5
144
145
}]
145
146
```
146
147
147
-
## Learn more about JSON in SQL Server and Azure SQL Database
148
-
149
-
### Microsoft videos
150
-
151
-
> [!NOTE]
152
-
> Some of the video links in this section may not work at this time. Microsoft is migrating content formerly on Channel 9 to a new platform. We will update the links as the videos are migrated to the new platform.
148
+
## Learn more about JSON in the SQL Database Engine
153
149
154
-
For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:
150
+
For a visual introduction to the built-in JSON support, see the following videos:
155
151
156
152
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven-SQLServer2016/JSON-as-bridge-betwen-NoSQL-relational-worlds)
Copy file name to clipboardExpand all lines: docs/relational-databases/json/convert-json-data-to-rows-and-columns-with-openjson-sql-server.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
1
---
2
-
title: "Parse and transform JSON data with OPENJSON"
2
+
title: "Parse and Transform JSON Data with OPENJSON"
3
3
description: OPENJSON converts JSON into a set of rows and columns. Use it to run any SQL query on the returned data, or insert it into a SQL Server table.
The `OPENJSON` rowset function converts JSON text into a set of rows and columns. After you transform a JSON collection into a rowset with `OPENJSON`, you can run any SQL query on the returned data or insert it into a SQL Server table. For more information about working with JSON data in the [!INCLUDE [ssdenoversion-md](../../includes/ssdenoversion-md.md)], see [JSON data in SQL Server](json-data-sql-server.md).
To format the output of the **FOR JSON** clause automatically based on the structure of the **SELECT** statement, specify the **AUTO** option.
19
-
20
-
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.
21
-
22
-
The alternative is to use the **PATH** option to maintain control over the output.
23
-
- For more info about the **PATH** option, see [Format Nested JSON Output with PATH Mode](../../relational-databases/json/format-nested-json-output-with-path-mode-sql-server.md).
24
-
- For an overview of both options, see [Format Query Results as JSON with FOR JSON](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
25
-
26
-
A query that uses the **FOR JSON AUTO** option must have a **FROM** clause.
27
-
28
-
Here are some examples of the **FOR JSON** clause with the **AUTO** option. [Azure Data Studio](/azure-data-studio/download-azure-data-studio) 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.
To format the output of the `FOR JSON` clause automatically based on the structure of the `SELECT` statement, specify the `AUTO` option.
19
+
20
+
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.
21
+
22
+
The alternative is to use the `PATH` option to maintain control over the output.
23
+
- 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).
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
+
26
+
A query that uses the `FOR JSON AUTO` option must have a `FROM` clause.
27
+
28
+
Here are some examples of the `FOR JSON` clause with the `AUTO` option. [Azure Data Studio](/azure-data-studio/download-azure-data-studio) 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.
29
+
30
30
## Examples
31
31
32
32
### Example 1
33
+
33
34
**Query**
34
-
35
-
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.
36
-
37
-
```sql
35
+
36
+
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.
37
+
38
+
```sql
38
39
SELECT TOP 5
39
40
BusinessEntityID As Id,
40
41
FirstName, LastName,
@@ -43,10 +44,10 @@ SELECT TOP 5
43
44
FROMPerson.Person
44
45
FOR JSON AUTO
45
46
```
46
-
47
+
47
48
**Result**
48
-
49
-
```json
49
+
50
+
```json
50
51
[{
51
52
"Id": 1,
52
53
"FirstName": "Ken",
@@ -77,10 +78,10 @@ SELECT TOP 5
77
78
### Example 2
78
79
79
80
**Query**
80
-
81
+
81
82
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.
82
-
83
-
```sql
83
+
84
+
```sql
84
85
SELECT TOP 2 SalesOrderNumber,
85
86
OrderDate,
86
87
UnitPrice,
@@ -90,10 +91,10 @@ FROM Sales.SalesOrderHeader H
90
91
ONH.SalesOrderID=D.SalesOrderID
91
92
FOR JSON AUTO
92
93
```
93
-
94
+
94
95
**Result**
95
-
96
-
```json
96
+
97
+
```json
97
98
[{
98
99
"SalesOrderNumber": "SO43659",
99
100
"OrderDate": "2011-05-31T00:00:00",
@@ -113,11 +114,11 @@ FOR JSON AUTO
113
114
```
114
115
115
116
### Example 3
116
-
117
+
117
118
**Query**
118
119
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.
119
-
120
-
```sql
120
+
121
+
```sql
121
122
SELECT TOP 2
122
123
SalesOrderNumber,
123
124
OrderDate,
@@ -128,10 +129,10 @@ SELECT TOP 2
128
129
FROMSales.SalesOrderHeaderAS H
129
130
FOR JSON PATH
130
131
```
131
-
132
+
132
133
**Result**
133
-
134
-
```json
134
+
135
+
```json
135
136
[{
136
137
"SalesOrderNumber": "SO43659",
137
138
"OrderDate": "2011-05-31T00:00:00",
@@ -147,13 +148,12 @@ FOR JSON PATH
147
148
}]
148
149
```
149
150
150
-
## Learn more about JSON in SQL Server and Azure SQL Database
151
-
152
-
### Microsoft videos
151
+
## Learn more about JSON in the SQL Database Engine
153
152
154
153
For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:
155
154
156
155
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven-SQLServer2016/JSON-as-bridge-betwen-NoSQL-relational-worlds)
0 commit comments