Sql Server Json处理

2018-06-06  本文已影响70人  凌雲木

json读取

DECLARE @JSON NVARCHAR(MAX)
SET @JSON=N'[
{"ID":1,"Name":"郑州","Lng":113.65,"Lat":34.76,"Count":269},
{"ID":2,"Name":"北京","Lng":116.46,"Lat":39.92,"Count":200},
{"ID":3,"Name":"天津","Lng":117.2,"Lat":39.13,"Count":100},
{"ID":4,"Name":"武汉","Lng":114.31,"Lat":30.52,"Count":199},
{"ID":5,"Name":"南京","Lng":118.78,"Lat":32.04,"Count":199},
{"ID":6,"Name":"上海","Lng":121.48,"Lat":31.22,"Count":200},
{"ID":7,"Name":"广州","Lng":113.23,"Lat":23.16,"Count":200},
{"ID":8,"Name":"成都","Lng":104.06,"Lat":30.67,"Count":199},
{"ID":9,"Name":"重庆","Lng":106.54,"Lat":29.59,"Count":200},
{"ID":10,"Name":"深圳","Lng":114.07,"Lat":22.62,"Count":200},
{"ID":11,"Name":"乌鲁木齐","Lng":87.68,"Lat":43.77,"Count":100}
]';

WITH [AddressJson] AS(
SELECT ID,
Name,
Lng,
Lat,
Count
FROM OPENJSON(@JSON)
WITH(
ID INT, 
Name NVARCHAR(10) '$.Name',
Lng FLOAT '$.Lng',
Lat FLOAT '$.Lat',
Count INT '$.Count'
))

SELECT * FROM [AddressJson]

1   郑州  113.65  34.76   269
2   北京  116.46  39.92   200
3   天津  117.2   39.13   100
4   武汉  114.31  30.52   199
5   南京  118.78  32.04   199
6   上海  121.48  31.22   200
7   广州  113.23  23.16   200
8   成都  104.06  30.67   199
9   重庆  106.54  29.59   200
10  深圳  114.07  22.62   200
11  乌鲁木齐    87.68   43.77   100
image.png

使用Josn统计各年龄段人数

DECLARE @JSON NVARCHAR(MAX)
SET
  @JSON = N'[
{"StartAge":0,"EndAge":"25"},
{"StartAge":26,"EndAge":"29"},
{"StartAge":30,"EndAge":"34"},
{"StartAge":35,"EndAge":"39"},
{"StartAge":40,"EndAge":"44"},
{"StartAge":45,"EndAge":"49"},
{"StartAge":50,"EndAge":"100"}
]';WITH [AddressJson] AS(
    SELECT
      StartAge,
      EndAge
    FROM
      OPENJSON(@JSON) WITH(StartAge INT, EndAge INT)
  )
SELECT
  [AddressJson].StartAge AS '起始年龄',
  [AddressJson].EndAge AS '结束年龄',
  COUNT(DISTINCT([dbo].[hrmresource].id)) AS '人数'
FROM
  [AddressJson]
  INNER JOIN [dbo].[hrmresource] ON (
    year(getdate()) - year([dbo].[hrmresource].birthday)
  ) BETWEEN [AddressJson].StartAge
  AND [AddressJson].EndAge
WHERE
  YEAR([dbo].[hrmresource].synctime) = 2019
  AND MONTH([dbo].[hrmresource].synctime) = 2
GROUP BY
  [AddressJson].StartAge,
  [AddressJson].EndAge
image.png

生成json

使用FOR JSON PATH 或 FOR JSON AUTO

SELECT [Name],[CreateTime] FROM [dbo].[User] FOR JSON AUTO

[
  {
    "Name": "abx",
    "CreateTime": "2000-01-01T00:00:00"
  },
  {
    "Name": "asd",
    "CreateTime": "2000-01-01T00:00:00"
  }
]

SELECT [Name],[CreateTime] FROM [dbo].[User] FOR JSON PATH

[
  {
    "Name": "abx",
    "CreateTime": "2000-01-01T00:00:00"
  },
  {
    "Name": "asd",
    "CreateTime": "2000-01-01T00:00:00"
  }
]

问题。 我希望在单个表上从简单的 SQL 查询创建 JSON 文本结果。 FOR JSON PATH 和 FOR JSON AUTO 生成相同的输出。 我应该使用这两个选项之中的哪一个?

答案。 请使用 FOR JSON PATH。 尽管 JSON 输出没有任何区别,但 AUTO 模式采用一些其他的逻辑,可检查是否应嵌套列。 请将 PATH 作为默认选项。

修改json
DECLARE @json NVARCHAR(MAX), @json1 NVARCHAR(MAX);
SET @json = '{"info":{"address":[{"town":"北京"},{"town":"上海"},{"town":"广州"}]}}';
SET @json1 = JSON_MODIFY(@json,'$.info.address[1].town','London');
SELECT modifiedJson = @json1;
image.png
上一篇下一篇

猜你喜欢

热点阅读