JSON

使用TSQL查询和更新 JSON 数据(2)

字号+ 作者:H5之家 来源:H5之家 2017-09-18 16:00 我要评论( )

declare @json nvarchar(max) set @json = N'{ info:{ type:1, address:{ town:bristol, county:avon, country:england }, tags:[sport, water polo] }, type:basic }' SELECT info_type,info_address,tags FROM OP

  • declare @json nvarchar(max)
  • set @json =
  • N'{
  • "info":{
  • "type":1,
  • "address":{
  • "town":"bristol",
  • "county":"avon",
  • "country":"england"
  • },
  • "tags":["sport", "water polo"]
  • },
  • "type":"basic"
  • }'
  • SELECT info_type,info_address,tags
  • FROM OPENJSON(@json, '$.info')
  • with
  • (
  • info_type tinyint 'lax $.type',
  • info_address nvarchar(max) 'lax $.address' as json,
  • tags nvarchar(max) 'lax $.tags' as json
  • )

    复制代码

    六,将关系表数据以JSON格式存储

    通过For JSON Auto/Path,将关系表数据存储为JSON格式,

  • Auto 模式:根据select语句中column的顺序,自动生成JSON数据的格式;
  • Path 模式:使用column name的格式来生成JSON数据的格式,column name使用逗号分隔(dot-separated)表示组-成员关系;
  • 示例,有表:dt_json,存储以下数据:

    1,以Auto 模式生成JSON格式

  • select id,
  • name,
  • category
  • from dbo.dt_json
  • for json auto,root('json')

    复制代码

    返回的数据格式是

  • {
  • "json":[
  • {
  • "id":1,
  • "name":"C#",
  • "category":"Computer"
  • },
  • {
  • "id":2,
  • "name":"English",
  • "category":"Language"
  • },
  • {
  • "id":3,
  • "name":"MSDN",
  • "category":"Web"
  • },
  • {
  • "id":4,
  • "name":"Blog",
  • "category":"Forum"
  • }
  • ]
  • }

    复制代码

    View Code

    2,以Path模式生成JSON格式

  • select id as 'book.id',
  • name as 'book.name',
  • category as 'product.category'
  • from dbo.dt_json
  • for json path,root('json')

    复制代码

    返回的数据格式是:

  • {
  • "json":[
  • {
  • "book":{
  • "id":1,
  • "name":"C#"
  • },
  • "product":{
  • "category":"Computer"
  • }
  • },
  • {
  • "book":{
  • "id":2,
  • "name":"English"
  • },
  • "product":{
  • "category":"Language"
  • }
  • },
  • {
  • "book":{
  • "id":3,
  • "name":"MSDN"
  • },
  • "product":{
  • "category":"Web"
  • }
  • },
  • {
  • "book":{
  • "id":4,
  • "name":"Blog"
  • },
  • "product":{
  • "category":"Forum"
  • }
  • }
  • ]
  • }

    复制代码

    View Code

    七,索引JSON数据

    JSON文本不是内置的数据类型,没有专门的JSON索引,但是,可以通过创建计算列和标准B-Tree索引提高查询JSON数据的性能,避免全表扫描(Full Table Scan),通过索引计算列,间接实现对JSON进行查找。

    索引JSON数据的Workaround是:为查询条件(Filter)创建计算列,使用persisted属性持久存储;在计算列上创建索引,使用包含列(Include)包含特定的字段,以避免键值查找(Key Lookup),提高索引查找的性能。

    例如,有如下关系表,字段category包含JSON数据:

    按照type属性过滤,包含name字段,创建索引的示例是:

  • alter table dbo.dt_json
  • add category_type as (cast(json_value(category,'$.type') as int)) persisted;
  • create nonclustered index idx_dt_json_category_type
  • on dbo.dt_json
  • (
  • category_type
  • )
  • include(name);

    复制代码

    参考文档:

    JSON Data (SQL Server)

    JSON Path Expressions (SQL Server)

    JSON Functions (Transact-SQL)

    OPENJSON (Transact-SQL)

    Index JSON data

    Format Query Results as JSON with FOR JSON (SQL Server)

    Format Nested JSON Output with PATH Mode (SQL Server)

    Format JSON Output Automatically with AUTO Mode (SQL Server)

    JSON Support in SQL Server 2016

    JSON in SQL Server 2016: Part 1 of 4

     

  • 1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • Golang 中使用 JSON 的小技巧

      Golang 中使用 JSON 的小技巧

      2017-09-18 15:18

    • litjson的生成和解析

      litjson的生成和解析

      2017-09-17 18:03

    • jQuery中读取json文件示例代码

      jQuery中读取json文件示例代码

      2017-09-17 10:00

    • json中换行符的处理方法示例介绍

      json中换行符的处理方法示例介绍

      2017-09-17 09:06

    网友点评