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
Copy file name to clipboardExpand all lines: docs/relational-databases/json/format-json-output-automatically-with-auto-mode-sql-server.md
+25-19Lines changed: 25 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: "Format JSON Output Automatically with AUTO Mode (SQL Server) | Microsoft Docs"
3
3
ms.custom:
4
4
- "SQL2016_New_Updated"
5
-
ms.date: "06/02/2016"
5
+
ms.date: "07/17/2017"
6
6
ms.prod: "sql-server-2016"
7
7
ms.reviewer: ""
8
8
ms.suite: ""
@@ -23,20 +23,22 @@ manager: "jhubbard"
23
23
24
24
To format the output of the **FOR JSON** clause automatically based on the structure of the **SELECT** statement, specify the **AUTO** option.
25
25
26
-
With 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.
26
+
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.
27
27
28
-
The alternative is to use the **PATH** option to maintain control over the output.
29
-
- 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).
30
-
- 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).
31
-
32
-
A query that uses the **FOR JSON AUTO** option must have a **FROM** clause.
28
+
The alternative is to use the **PATH** option to maintain control over the output.
29
+
- 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).
30
+
- 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).
31
+
32
+
A query that uses the **FOR JSON AUTO** option must have a **FROM** clause.
33
33
34
-
Here are some examples of the **FOR JSON** clause with the **AUTO** option.
34
+
Here are some examples of the **FOR JSON** clause with the **AUTO** option.
35
35
36
-
## Examples
37
-
**Query 1**
36
+
## Examples
37
+
38
+
### Example 1
39
+
**Query**
38
40
39
-
The results of the FOR JSON AUTO clause are similar to the results of FOR JSON PATH when only one table is used in the query. 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.
41
+
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.
40
42
41
43
```sql
42
44
SELECT TOP 5
@@ -48,7 +50,7 @@ SELECT TOP 5
48
50
FOR JSON AUTO
49
51
```
50
52
51
-
**Result 1**
53
+
**Result**
52
54
53
55
```json
54
56
[{
@@ -77,10 +79,12 @@ SELECT TOP 5
77
79
"Info.MiddleName": "A"
78
80
}]
79
81
```
82
+
83
+
### Example 2
84
+
85
+
**Query**
80
86
81
-
**Query 2**
82
-
83
-
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.
84
88
85
89
```sql
86
90
SELECT TOP 2 SalesOrderNumber,
@@ -93,7 +97,7 @@ FROM Sales.SalesOrderHeader H
93
97
FOR JSON AUTO
94
98
```
95
99
96
-
**Result 2**
100
+
**Result**
97
101
98
102
```json
99
103
[{
@@ -113,9 +117,11 @@ FOR JSON AUTO
113
117
}]
114
118
}]
115
119
```
120
+
121
+
### Example 3
116
122
117
-
**Query 3**
118
-
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
+
**Query**
124
+
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
125
120
126
```sql
121
127
SELECT TOP 2
@@ -129,7 +135,7 @@ FROM Sales.SalesOrderHeader AS H
To maintain full control over the output of the **FOR JSON** clause, specify the **PATH** option.
22
+
To maintain full control over the output of the **FOR JSON** clause, specify the **PATH** option.
23
23
24
-
**PATH** mode lets you create wrapper objects and nest complex properties. The results are formatted as an array of JSON objects.
24
+
**PATH** mode lets you create wrapper objects and nest complex properties. The results are formatted as an array of JSON objects.
25
25
26
26
The alternative is to use the **AUTO** option to format the output automatically based on the structure of the **SELECT** statement.
27
27
- For more info about the **AUTO** option, see [Format JSON Output Automatically with AUTO Mode](../../relational-databases/json/format-json-output-automatically-with-auto-mode-sql-server.md) .
@@ -30,9 +30,9 @@ The alternative is to use the **AUTO** option to format the output automatically
30
30
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.
31
31
32
32
## Example - Dot-separated column names
33
-
The following query formats the first five rows from the AdventureWorks Person table as JSON.
33
+
The following query formats the first five rows from the AdventureWorks `Person` table as JSON.
34
34
35
-
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.
35
+
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.
36
36
37
37
**Query**
38
38
@@ -52,7 +52,7 @@ SELECT TOP 5
52
52
[{
53
53
"Id": 1,
54
54
"FirstName": "Ken",
55
-
"LastName": "Sánchez",
55
+
"LastName": "Sanchez",
56
56
"Info": {
57
57
"MiddleName": "J"
58
58
}
@@ -83,7 +83,7 @@ SELECT TOP 5
83
83
```
84
84
85
85
## Example - Multiple tables
86
-
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.
86
+
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.
The built-in support for JSON includes the following built-in functions described in this topic.
25
+
The built-in support for JSON includes the following built-in functions described briefly in this topic.
26
26
27
27
-[ISJSON](#ISJSON) tests whether a string contains valid JSON.
28
28
@@ -35,7 +35,7 @@ manager: "jhubbard"
35
35
## JSON text for the examples on this page
36
36
The examples on this page use the following JSON text, which contains a complex element.
37
37
38
-
```json
38
+
```sql
39
39
DECLARE @jsonInfo NVARCHAR(MAX)
40
40
41
41
SET @jsonInfo=N'{
@@ -55,44 +55,44 @@ SET @jsonInfo=N'{
55
55
## <aname="ISJSON"></a> Validate JSON text by using the ISJSON function
56
56
The **ISJSON** function tests whether a string contains valid JSON.
57
57
58
-
The following example returns the JSON text if the column contains valid JSON.
58
+
The following example returns rows in which the column`json_col` contains valid JSON.
59
59
60
60
```sql
61
-
SELECT id,json_col
61
+
SELECT id,json_col
62
62
FROM tab1
63
-
WHERE ISJSON(json_col)>0
63
+
WHERE ISJSON(json_col)>0
64
64
```
65
-
66
-
For more info, see [ISJSON (Transact-SQL)](../../t-sql/functions/isjson-transact-sql.md).
65
+
66
+
For more info, see [ISJSON (Transact-SQL)](../../t-sql/functions/isjson-transact-sql.md).
67
67
68
68
## <aname="VALUE"></a> Extract a value from JSON text by using the JSON_VALUE function
69
-
The **JSON_VALUE** function extracts a scalar value from a JSON string.
69
+
The **JSON_VALUE** function extracts a scalar value from a JSON string.
70
70
71
-
The following example extracts the value of a JSON property into a local variable.
71
+
The following example extracts the value of the nested JSON property`town` into a local variable.
72
72
73
73
```sql
74
-
SET @town=JSON_VALUE(@jsonInfo,'$.info.address.town')
74
+
SET @town=JSON_VALUE(@jsonInfo,'$.info.address.town')
75
75
```
76
76
77
-
For more info, see [JSON_VALUE (Transact-SQL)](../../t-sql/functions/json-value-transact-sql.md).
77
+
For more info, see [JSON_VALUE (Transact-SQL)](../../t-sql/functions/json-value-transact-sql.md).
78
78
79
79
## <aname="QUERY"></a> Extract an object or an array from JSON text by using the JSON_QUERY function
80
-
The **JSON_QUERY** function extracts an object or an array from a JSON string.
80
+
The **JSON_QUERY** function extracts an object or an array from a JSON string.
81
81
82
-
The following example shows how to return a JSON fragment in query results.
82
+
The following example shows how to return a JSON fragment in query results.
83
83
84
84
```sql
85
-
SELECT FirstName,LastName,JSON_QUERY(jsonInfo,'$.info.address') AS Address
85
+
SELECT FirstName,LastName,JSON_QUERY(jsonInfo,'$.info.address') AS Address
86
86
FROMPerson.Person
87
87
ORDER BY LastName
88
88
```
89
89
90
-
For more info, see [JSON_QUERY (Transact-SQL)](../../t-sql/functions/json-query-transact-sql.md).
90
+
For more info, see [JSON_QUERY (Transact-SQL)](../../t-sql/functions/json-query-transact-sql.md).
91
91
92
92
## <aname="JSONCompare"></a> Compare JSON_VALUE and JSON_QUERY
93
-
The key difference between **JSON_VALUE** and **JSON_QUERY** is that **JSON_VALUE** returns a scalar value, while **JSON_QUERY** returns an object or an array.
93
+
The key difference between **JSON_VALUE** and **JSON_QUERY** is that **JSON_VALUE** returns a scalar value, while **JSON_QUERY** returns an object or an array.
94
94
95
-
Consider the following sample JSON text.
95
+
Consider the following sample JSON text.
96
96
97
97
```json
98
98
{
@@ -102,9 +102,9 @@ ORDER BY LastName
102
102
}
103
103
```
104
104
105
-
In this sample JSON text, data members "a" and "c" are string values, while data member "b" is an array. **JSON_VALUE** and **JSON_QUERY** return the following results:
105
+
In this sample JSON text, data members "a" and "c" are string values, while data member "b" is an array. **JSON_VALUE** and **JSON_QUERY** return the following results:
|**$**|NULL or error|`{ "a": "[1,2]", "b": [1,2], "c":"hi"}`|
110
110
|**$.a**|[1,2]|NULL or error|
@@ -113,27 +113,27 @@ ORDER BY LastName
113
113
|**$.c**|hi|NULL or error|
114
114
115
115
## Test JSON_VALUE and JSON_QUERY with the AdventureWorks sample database
116
-
Test the built-in functions described in this topic by running the following examples with the AdventureWorks sample database, which contains JSON data. To get the AdventureWorks sample database, [click here](http://www.microsoft.com/en-us/download/details.aspx?id=49502).
116
+
Test the built-in functions described in this topic by running the following examples with the AdventureWorks sample database, which contains JSON data. To get the AdventureWorks sample database, [click here](http://www.microsoft.com/en-us/download/details.aspx?id=49502).
117
117
118
-
In the following examples, the Info column in the SalesOrder_json table contains JSON text.
118
+
In the following examples, the `Info` column in the `SalesOrder_json` table contains JSON text.
119
119
120
120
### Example 1 - Return both standard columns and JSON data
121
-
The following query returns both standard relational columns and values from a JSON column.
121
+
The following query returns values from both standard relational columns and from a JSON column.
The following query aggregates subtotals by customer name (stored in JSON) and status (stored in an ordinary column). Then it filters the results by city (stored in JSON), and OrderDate (stored in an ordinary column).
136
+
The following query aggregates subtotals by customer name (stored in JSON) and status (stored in an ordinary column). Then it filters the results by city (stored in JSON) and OrderDate (stored in an ordinary column).
137
137
138
138
```sql
139
139
DECLARE @territoryid INT;
@@ -143,22 +143,22 @@ SET @territoryid=3;
143
143
144
144
SET @city=N'Seattle';
145
145
146
-
SELECT JSON_VALUE(Info,'$.Customer.Name') AS Customer,Status,SUM(SubTotal) AS Total
146
+
SELECT JSON_VALUE(Info,'$.Customer.Name') AS Customer,Status,SUM(SubTotal) AS Total
147
147
FROMSales.SalesOrder_json
148
148
WHERE TerritoryID=@territoryid
149
-
AND JSON_VALUE(Info,'$.ShippingInfo.City')=@city
150
-
AND OrderDate>'1/1/2015'
151
-
GROUP BY JSON_VALUE(Info,'$.Customer.Name'),Status
149
+
AND JSON_VALUE(Info,'$.ShippingInfo.City')=@city
150
+
AND OrderDate>'1/1/2015'
151
+
GROUP BY JSON_VALUE(Info,'$.Customer.Name'),Status
152
152
HAVINGSUM(SubTotal)>1000
153
153
```
154
154
155
155
## <aname="MODIFY"></a> Update property values in JSON text by using the JSON_MODIFY function
156
-
The **JSON_MODIFY** function updates the value of a property in a JSON string and returns the updated JSON string.
156
+
The **JSON_MODIFY** function updates the value of a property in a JSON string and returns the updated JSON string.
157
157
158
-
The following example updates the value of a property in a variable that contains JSON.
158
+
The following example updates the value of a JSON property in a variable that contains JSON.
159
159
160
160
```sql
161
-
SET @info=JSON_MODIFY(@jsonInfo,"$.info.address[0].town",'London')
161
+
SET @info=JSON_MODIFY(@jsonInfo,"$.info.address[0].town",'London')
162
162
```
163
163
164
164
For more info, see [JSON_MODIFY (Transact-SQL)](../../t-sql/functions/json-modify-transact-sql.md).
0 commit comments