JSON in SQL Server 2016

JSON functions in SQL Server enable you to analyze and query JSON data, transform JSON to relational format, and export SQL query results as JSON text.

If you have JSON text, you can extract data from JSON or verify that JSON is properly formatted using built-in functions JSON_VALUE, JSON_QUERY, and ISJSON. For more advanced querying and analysis, the OPENJSON function can transform an array of JSON objects into a set of rows. Any SQL query can be executed on the returned result set. Finally, there is the FOR JSON clause that enables you to format query results as JSON text.

SELECT TOP 1000 [Version]
      ,[OSType]
      ,[Online]
      ,[OnlineT]
  FROM [ApplicationTest].[dbo].[table1]
  for json auto

  

We can start with simple examples. In the following Transact-SQL code, we will define a text variable where we will put JSON text:

DECLARE @json NVARCHAR(4000)
SET @json =
N‘{
    "info":{
      "type":1,

      "address":{
        "town":"Bristol",
        "county":"Avon",
        "country":"England"
      },
      "tags":["Sport", "Water polo"]
   },
   "type":"Basic"
}‘

  

Now, we can extract values and objects from JSON text using the JSON_VALUE and JSON_QUERY functions:

SELECT
  JSON_VALUE(@json, ‘$.type‘) as type,
  JSON_VALUE(@json, ‘$.info.address.town‘) as town,
  JSON_QUERY(@json, ‘$.info.tags‘) as tags

  

This query will return “Basic”, “Bristol”, and ["Sport", "Water polo"] values. The JSON_VALUE function returns one scalar value from JSON text (e.g. strings, numbers, true/false) that is placed on a JSON path specified as the second parameter. JSON_QUERY returns an object or array (in this example an array of tags) on the JSON path. JSON built-in functions use JavaScript-like syntax to reference values and objects in JSON text via second parameter.

The OPENJSON function enables you to reference some array in JSON text and return elements from that array:

SELECT value
FROM OPENJSON(@json, ‘$.info.tags‘)

INSERT INTO Orders(Number, Date, Customer, Quantity)
SELECT Number, Date, Customer, Quantity
 OPENJSON (@orders)
 WITH (
        Number varchar(200),
        Date datetime,
        Customer varchar(200),
        Quantity int
 ) AS OrdersArray

  

Four columns in the result set that is returned by OPENJSON are defined in the WITH clause. OPENJSON will try to find the properties Number, Date, Customer, and Quantity in each JSON object and convert their values into columns in the result set. By default, NULL will be returned if the property is not found. The assumption in the query above is that the @orders variable contains the following JSON array:

‘[
   {"Number":1, "Date": "8/10/2012", "Customer": "Adventure works", "Quantity": 1200},
   {"Number":4, "Date": "5/11/2012", "Customer": "Adventure works", "Quantity": 100},
   {"Number":6, "Date": "1/3/2012", "Customer": "Adventure works", "Quantity": 250},
   {"Number":8, "Date": "12/7/2012", "Customer": "Adventure works", "Quantity": 2200}
]‘

  

As you can see, the transformation from a JSON text to a relational form is simple. You just need to specify column names and types and OPENJSON will find properties in JSON that match these columns. In this example, plain JSON is used; however, OPENJSON can handle any nested/hierarchical structure of JSON objects.

Also, OPENJSON can be used to combine relational and JSON data in the same query. If we assume that the JSON array shown in the previous example is stored in the Orders column, the following query can combine the columns and JSON fields:

SELECT Id, FirstName, LastName, Number, Date, Customer, Quantity
 FROM Person
    CROSS APPLY OPENJSON (OrdersJson)
                            WITH (
                                        Number varchar(200),
                                        Date datetime,
                                        Customer varchar(200),
                                        Quantity int ) AS OrdersArray

  

时间: 2024-10-08 23:07:59

JSON in SQL Server 2016的相关文章

sql server 2016 json 解析方法

前几天发现了sql server 2016支持了json 项目需要所以安装了 用了一下 方便了很多  ,写一下小笔记方便日后查看,也希望各位大神指正共同学习. sql server 2016 安装图解网上很多,大家注意安装版本就可以了. --1 带 Root Keyselect * from sys_menu for JSON AUTO ,ROOT('Result') --2  asselect ID, Name, Age as [Entity.Age], Sex as [Entity.Sex]

在Sql Server 2016中使用For Json子句把数据作为json格式导出

原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/export-query-result-as-json-format-in-sql-server-2016/ 使用for json子句把查询结果作为json字符串导出,将作为sql server 2016中首先可用的一个特性.如果你熟悉for xml子句,那么将很容易理解for json: select ccolumn, expression, column as ali

Sql Server 2016中增加了对JSON的内置支持

原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/built-in-json-support-in-sql-server-2016/ 在数据库层对JSON提供支持,是请求排名最靠前的特性之一,在Microsoft Connect网站上对他的投票量超过了1000.微软承诺,在Sql Server 2016版本中,会提供内置的JSON支持.注意这并不是Sql Server 2005已有特性XML原生支持的翻版.微软的目标是创

SQL Server 2016 JSON原生支持实例说明

原文:SQL Server 2016 JSON原生支持实例说明 背景 Microsoft SQL Server 对于数据平台的开发者来说越来越友好.比如已经原生支持XML很多年了,在这个趋势下,如今也能在SQLServer2016中使用内置的JSON.尤其对于一些大数据很数据接口的解析环节来说这显得非常有价值.与我们现在所做比如在SQL中使用CLR或者自定义的函数来解析JSON相比较,新的内置JSON会大大提高性能,同时优化了编程以及增删查改等方法. 那么是否意味着我们可以丢弃XML,然后开始使

SQL Server 2016 ->> T-SQL新特性

1) TRUNCATE表分区而不是整表 CREATE TABLE dbo.TruncatePartitionTest ( PrtCol INT, Col2 NVARCHAR(300) ) ON [myPS1](PrtCol) GO INSERT dbo.TruncatePartitionTest VALUES(1,'AAA'), (11,'AAA'), (100,'AAA'), (101,'AAA') GO -- TRUNCATE partitions 1 to 2 TRUNCATE TABLE

微软发布正式版SQL Server 2016

微软于今天在SQL 官方博客上宣布 SQL Server 数据库软件的正式发布版本(GA),历时一年多,微软为该软件发布了多个公共预览版和候选版本,而今天最终版本终于上线了.在博客中,微软数据集团的企业副总裁 Joseph Sirosh 表示:“在已经简化的企业数据管理基础上 SQL Server 2016 再次简化了数据库分析方式,强化分析来深入接触那些需要管理的数据.”在保持售价不变的情况下,Sirosh 表示将会增加性能和功能扩展. SQL Server 2016 数据库系统对于云数据管理

SQL Server 2016 CTP2 发布

SQL Server 2016 CTP2 于5月29日正式发布 Community Technology Preview(社区技术预览版)仅用于测试目的,不应在生产环境中安装和使用.在生产环境中安装正式版本时,会强制要求先卸载本机的所有CTP版本. CTP计划是微软建立新形象的一个策略,CTP计划使用一种"早发布,常发布"的方式来让用户更及时的实际测试软件:创建独立的"feature crews(功能小组)",可以迅速的创建特定的功能,并且针对这些功能直接与用户交流

下载:SQL Server 2016 CTP3 (x64) - DVD (English)

如题: 下载地址,请看文章底部. SQL Server 2016 CTP3 (x64) - DVD (English)ISO English Release Date: 10/30/2015 Details No product key is required. Download 2649 MB File Name:en_sql_server_2016_ctp3_x64_dvd_7213912.isoLanguages:EnglishSHA1:BF0FDF34B5A4BA9660C6CDB70E

SQL Server 2016 AlwaysOn 安装及配置介绍

SQL Server 2016  AlwaysOn 安装及配置介绍 Always On 可用性组功能是一个提供替代数据库镜像的企业级方案的高可用性和灾难恢复解决方案. SQL Server 2012 中引入了 Always On 可用性组功能,此功能可最大程度地提高一组用户数据库对企业的可用性. "可用性组" 针对一组离散的用户数据库(称为"可用性数据库" ,它们共同实现故障转移)支持故障转移环境. 一个可用性组支持一组读写主数据库以及一至八组对应的辅助数据库. (