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
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.
24
24
@@ -37,9 +37,10 @@ When you use the **OPENJSON** function without providing an explicit schema for
37
37
**OPENJSON** returns each property of the JSON object, or each element of the array, as a separate row.
38
38
39
39
Here's a quick example that uses **OPENJSON** with the default schema - that is, without the optional **WITH** clause - and returns one row for each property of the JSON object.
40
-
40
+
41
41
**Example**
42
-
```sql
42
+
43
+
```sql
43
44
DECLARE @json NVARCHAR(MAX)
44
45
45
46
SET @json='{"name":"John","surname":"Doe","age":45,"skills":["SQL","C#","MVC"]}';
@@ -48,7 +49,7 @@ SELECT *
48
49
FROM OPENJSON(@json);
49
50
```
50
51
51
-
**Results**
52
+
**Results**
52
53
53
54
|key|value|type|
54
55
|---------|-----------|----------|
@@ -63,8 +64,8 @@ For more info and examples, see [Use OPENJSON with the Default Schema (SQL S
63
64
64
65
For syntax and usage, see [OPENJSON (Transact-SQL)](../../t-sql/functions/openjson-transact-sql.md).
65
66
66
-
67
67
## Option 2 - OPENJSON output with an explicit structure
68
+
68
69
When you specify a schema for the results by using the **WITH** clause of the **OPENJSON** function, the function returns a table with only the columns that you define in the **WITH** clause. In the optional **WITH** clause, you specify a set of output columns, their types, and the paths of the JSON source properties for each output value. **OPENJSON** iterates through the array of JSON objects, reads the value on the specified path for each column, and converts the value to the specified type.
69
70
70
71
Here's a quick example that uses **OPENJSON** with a schema for the output that you explicitly specify in the **WITH** clause.
@@ -123,11 +124,13 @@ This function returns and formats the elements of a JSON array.
123
124
- For each column, specified by using the `colName type json_path` syntax, **OPENJSON** converts the value found in each array element on the specified path to the specified type. In this example, values for the `Date` column are taken from each element on the path `$.Order.Date` and converted to datetime values.
124
125
125
126
### More info about OPENJSON with an explicit schema
127
+
126
128
For more info and examples, see [Use OPENJSON with an Explicit Schema (SQL Server)](../../relational-databases/json/use-openjson-with-an-explicit-schema-sql-server.md).
127
129
128
130
For syntax and usage, see [OPENJSON (Transact-SQL)](../../t-sql/functions/openjson-transact-sql.md).
129
131
130
132
## OPENJSON requires Compatibility Level 130
133
+
131
134
The **OPENJSON** function is available only under **compatibility level 130**. If your database compatibility level is lower than 130, SQL Server can't find and run the **OPENJSON** function. Other built-in JSON functions are available at all compatibility levels.
132
135
133
136
You can check compatibility level in the `sys.databases` view or in database properties.
@@ -141,13 +144,12 @@ You can change the compatibility level of a database by using the following comm
141
144
142
145
For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:
143
146
144
-
-[SQL Server 2016 and JSON Support](https://channel9.msdn.com/Shows/Data-Exposed/SQL-Server-2016-and-JSON-Support)
147
+
-[SQL Server 2016 and JSON Support](https://channel9.msdn.com/Shows/Data-Exposed/SQL-Server-2016-and-JSON-Support)
145
148
146
-
-[Using JSON in SQL Server 2016 and Azure SQL Database](https://channel9.msdn.com/Shows/Data-Exposed/Using-JSON-in-SQL-Server-2016-and-Azure-SQL-Database)
149
+
-[Using JSON in SQL Server 2016 and Azure SQL Database](https://channel9.msdn.com/Shows/Data-Exposed/Using-JSON-in-SQL-Server-2016-and-Azure-SQL-Database)
147
150
148
-
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven/SQLServer2016/JSON-as-a-bridge-betwen-NoSQL-and-relational-worlds)
151
+
-[JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven/SQLServer2016/JSON-as-a-bridge-betwen-NoSQL-and-relational-worlds)
JSON is a popular textual data format that's used for exchanging data in modern web and mobile applications. JSON is also used for storing unstructured data in log files or NoSQL databases such as Microsoft Azure Cosmos DB. Many REST web services return results that are formatted as JSON text or accept data that's formatted as JSON. For example, most Azure services, such as Azure Search, Azure Storage, and Azure Cosmos DB, have REST endpoints that return or consume JSON. JSON is also the main format for exchanging data between webpages and web servers by using AJAX calls.
24
24
25
25
JSON functions in SQL Server enable you to combine NoSQL and relational concepts in the same database. Now you can combine classic relational columns with columns that contain documents formatted as JSON text in the same table, parse and import JSON documents in relational structures, or format relational data to JSON text. You see how JSON functions connect relational and NoSQL concepts in SQL Server and Azure SQL Database in the following video:
26
26
27
27
*JSON as a bridge between NoSQL and relational worlds*
@@ -172,6 +172,10 @@ The result of this query is shown in the following table:
172
172
`OUTER APPLY OPENJSON` will join first level entity with sub-array and return flatten resultset. Due to JOIN, the second row will be repeated for every skill.
173
173
174
174
### Convert SQL Server data to JSON or export JSON
175
+
176
+
>[!NOTE]
177
+
>Converting Azure SQL Data Warehouse data to JSON or exporting JSON is not supported.
178
+
175
179
Format SQL Server data or the results of SQL queries as JSON by adding the **FOR JSON** clause to a **SELECT** statement. Use **FOR JSON** to delegate the formatting of JSON output from your client applications to SQL Server. For more information, see [Format Query Results as JSON with FOR JSON (SQL Server)](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
176
180
177
181
The following example uses PATH mode with the **FOR JSON** clause:
@@ -327,11 +331,11 @@ If you have a web service that takes data from the database layer and returns it
327
331
328
332
For example, you might want to generate JSON output that's compliant with the OData specification. The web service expects a request and response in the following format:
This OData URL represents a request for the ProductID and ProductName columns for the product with `id` 1. You can use **FOR JSON** to format the output as expected in SQL Server.
338
+
This OData URL represents a request for the ProductID and ProductName columns for the product with `ID` 1. You can use **FOR JSON** to format the output as expected in SQL Server.
Use the functions described on the pages in this section to validate or change JSON text or to extract simple or complex values.
22
24
@@ -28,10 +30,9 @@ Use the functions described on the pages in this section to validate or change J
28
30
|[JSON_MODIFY](../../t-sql/functions/json-modify-transact-sql.md)|Updates the value of a property in a JSON string and returns the updated JSON string.|
29
31
30
32
For more info about the built-in support for JSON in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], see [JSON Data (SQL Server)](../../relational-databases/json/json-data-sql-server.md).
31
-
32
-
## See Also
33
-
[Validate, Query, and Change JSON Data with Built-in Functions (SQL Server)](../../relational-databases/json/validate-query-and-change-json-data-with-built-in-functions-sql-server.md)
[JSON Data (SQL Server)](../../relational-databases/json/json-data-sql-server.md)
36
-
37
-
33
+
34
+
## See Also
35
+
36
+
-[Validate, Query, and Change JSON Data with Built-in Functions (SQL Server)](../../relational-databases/json/validate-query-and-change-json-data-with-built-in-functions-sql-server.md)
0 commit comments