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

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.

主站蜘蛛池模板: 阿拉善左旗| 四子王旗| 淅川县| 简阳市| 崇阳县| 桃江县| 井冈山市| 皮山县| 平江县| 马关县| 申扎县| 民县| 福州市| 商城县| 孝感市| 高阳县| 延吉市| 建瓯市| 安岳县| 天等县| 万全县| 灯塔市| 渭南市| 南昌县| 桐梓县| 南宫市| 会东县| 齐齐哈尔市| 包头市| 昌宁县| 安泽县| 改则县| 望都县| 乌拉特中旗| 栖霞市| 牟定县| 谷城县| 东乡县| 呼和浩特市| 岳普湖县| 江川县|