In my article, Warehousing JSON Formatted Data in SQL Server 2016, we had a look at available T-SQL options for converting JSON data into rows and columns for the purposes of populating a SQL Server based data warehouse. The increased popularity of JSON in modern web applications may create a requirement for data teams to expose some of their data to client applications (i.e. reporting tools, web services etc.) in a JSON format. In this article we take a look at how such a requirement can be implemented by data teams using SQL Server 2016 FOR JSON clause
在我的文章《 在SQL Server 2016中存储JSON格式的数据》中 ,我们介绍了可用于将JSON数据转换为行和列的T-SQL选项,以填充基于SQL Server的数据仓库。 JSON在现代Web应用程序中的日益普及可能会要求数据团队以JSON格式向客户端应用程序(即报告工具,Web服务等)公开其某些数据。 在本文中,我们将研究数据团队如何使用SQL Server 2016 FOR JSON子句来实现这种要求
SQL Server到JSON支持的数据类型 (SQL Server to JSON Supported Data Types)
Like many of the features in SQL Server, there are terms and conditions to using them and JSON is no different. Thus, it is important that we take note of the supported data types. SQL Server data stored in the following data types cannot be converted into JSON:
像SQL Server中的许多功能一样,有使用它们的条款和条件,JSON也不例外。 因此,重要的是要注意支持的数据类型。 无法将以以下数据类型存储SQL Server数据转换为JSON:
A breakdown of supported data types is shown in Table1
表1显示了支持的数据类型的细分
SQL Server Data Type | JSON Data Type |
---|---|
char, nchar, varchar, nvarchar, date, datetime, datetime2, time, datetimeoffset, uniqueidentifier, money | string |
int, bigint, float, decimal, numeric | number |
Bit | Boolean |
varbinary, binary, image, timestamp, rowversion | BASE64-encoded string |
SQL Server数据类型 | JSON数据类型 |
---|---|
char,nchar,varchar,nvarchar,日期,datetime,datetime2,时间,datetimeoffset,uniqueidentifier,货币 | 串 |
int,bigint,float,十进制,数字 | 数 |
位 | 布尔型 |
varbinary,binary,image,timestamp,rowversion | BASE64编码的字符串 |
FOR JSON T-SQL子句 (FOR JSON T-SQL Clause)
Although SQL Server’s support for XML allowed for graphical representation of the data via an editor (shown in Figure 1), attempting to view JSON data via an editor may be frustrating as JSON data is shown as an unformatted single row.
尽管SQL Server对XML的支持允许通过编辑器以图形方式表示数据(如图1所示),但尝试通过编辑器查看JSON数据可能会令人沮丧,因为JSON数据显示为未格式化的单行。