官术网_书友最值得收藏!

Retrieve data as JSON

When we're working with our tables in the database, we might need to convert the results to JSON format. This works very similarly to the XML conversions. SQL Server has a directive for the SELECT command FOR JSON.

You can run the following query to get the JSON document:

SELECT
[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[JobTitle]
,[PhoneNumber]
,[PhoneNumberType]
,[EmailAddress]
FROM [AdventureWorks2016].[HumanResources].[vEmployee]
FOR JSON AUTO

The result of such a query would be a JSON set of documents representing each employee record in the following format:

{"FirstName":"Ken","MiddleName":"J","LastName":"Sánchez","JobTitle":"Chief Executive Officer","PhoneNumber":"697-555-0142","PhoneNumberType":"Cell","EmailAddress":"ken0@adventure-works.com"}

Unlike with XML, with JSON, you have only two available modes, which can be used to process the query and transform the result set into JSON output. The modes are as follows:

  • FOR JSON PATH
  • FOR JSON AUTO

The first one offers you a way to modify the look of the results, which allows you to control how the wrapping and more complex outputs will be produced. With JSON AUTO, SQL Server will automatically format the JSON output based on the structure of the SELECT query you have executed.

主站蜘蛛池模板: 庐江县| 五指山市| 孟连| 宜兰县| 依安县| 陆良县| 新巴尔虎左旗| 抚顺县| 吕梁市| 嘉定区| 泰兴市| 高平市| 杨浦区| 邯郸市| 金秀| 衡阳县| 安吉县| 游戏| 奉节县| 株洲县| 鄂托克前旗| 中卫市| 乐亭县| 桦南县| 上饶市| 商南县| 德惠市| 周口市| 杭州市| 中卫市| 安塞县| 资溪县| 双牌县| 休宁县| 屏山县| 延津县| 秦安县| 德令哈市| 宿州市| 信丰县| 崇文区|