【问题标题】:Reading nested JSON returns NULL读取嵌套 JSON 返回 NULL
【发布时间】:2021-11-17 19:28:14
【问题描述】:

我查看了多个看似相似但找不到答案的答案。目前我正在处理这些数据:

[{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[10.74, 50.67], [7.75, 5.21]]}, "HQ": "X", "City": "X"},  
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[11.66, 49.55], [1.73, 5.05]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[12.91, 50.98], [2.72, 8.29]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[4.65, 50.39], [3.53, 6.29]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[4.48, 50.32], [4.42, 4.79]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.05, 49.06], [5.99, 4.51]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.65, 50.41], [6.77, 5.94]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.65, 50.41], [7.77, 4.94]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.74, 50.73], [8.72, 3.23]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[9.65, 50.19], [9.26, 5.68]]}, "HQ": "X", "City": "X"}]

解析后使用:

declare @data nvarchar(max) = (SELECT *  FROM [dbo].[Temp_Database_01] for json path)

select *
from  
   openjson(@data) 
   with (trade_area nvarchar(max) '$.Trade_Area')

我得到了以下内容:

                                       trade_area
  {"type": "Polygon", "coordinates": [[10.74, ...
  {"type": "Polygon", "coordinates": [[11.66, ...
  {"type": "Polygon", "coordinates": [[2.91,  ...
  {"type": "Polygon", "coordinates": [[4.65,  ...
  {"type": "Polygon", "coordinates": [[4.48,  ...
  {"type": "Polygon", "coordinates": [[6.05, 4...
  {"type": "Polygon", "coordinates": [[6.65,  ...
  {"type": "Polygon", "coordinates": [[6.65,  ...
  {"type": "Polygon", "coordinates": [[6.74,  ...
  {"type": "Polygon", "coordinates": [[9.65,  ...

现在,我想深入研究 JSON 并通过尝试获取 type 值来访问 Trade_Area,该值是此示例数据的 Polygon:

declare @data nvarchar(max) = (SELECT *  FROM [dbo].[Temp_Database_01] for json path)

select *
from  
   openjson(@data) 
   with (trade_area nvarchar(max) '$.Trade_Area.type')

但是,我收到所有行的 NULL。我究竟做错了什么?我的理想输出是将两个值分成两列:

      type                      coordinates
0  Polygon  [[10.74, 50.67], [7.75, 5.21]]
1  Polygon  [[11.66, 49.55], [1.73, 5.05]]
2  Polygon   [[12.91, 50.98], [2.72, 8.29]]
3  Polygon   [[4.65, 50.39], [3.53, 6.29]]
4  Polygon  [[4.48, 50.32], [4.48, 4.79]]
5  Polygon   [[6.05, 49.06], [5.05, 4.51]]
6  Polygon  [[6.65, 50.41], [6.65, 5.94]]
7  Polygon  [[6.65, 50.41], [7.65, 4.94]]
8  Polygon   [[6.74, 50.73], [8.72, 3.23]]
9  Polygon  [[9.65, 50.19], [9.26, 5.68]]

【问题讨论】:

  • 那不是 JSON。在 JSON 中,字符串必须由 " 包围。 json.org/json-en.html
  • 对不起,大卫,它实际上是,我抄录它来消毒它并错过了。它实际上有"。我会编辑它

标签: sql json sql-server tsql


【解决方案1】:

如果你想要密钥(序列),只需稍微扭曲

select A.[key]
      ,B.[Type]
      ,B.coordinates
from   openjson(@data) A
Cross Apply openjson(A.Value) with ( type        nvarchar(100) '$.Trade_Area.type'
                                    ,coordinates nvarchar(max) '$.Trade_Area.coordinates' as json
                                   ) B

结果

【讨论】:

  • 非常感谢您的回答,在这种特殊情况下我没有用它,但它肯定会派上用场。
  • @CeliusStingher 如果您不需要密钥,那么查理的答案可能会更高效
  • 谢谢,我接受了他,因为它确实帮助我解决了问题,但是了解这些额外的知识是一件好事。
  • @CeliusStingher 总是一件好事 :)
【解决方案2】:

这个

declare @data nvarchar(max) = '

[{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[10.74, 50.67], [7.75, 5.21]]}, "HQ": "X", "City": "X"},  
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[11.66, 49.55], [1.73, 5.05]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[12.91, 50.98], [2.72, 8.29]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[4.65, 50.39], [3.53, 6.29]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[4.48, 50.32], [4.42, 4.79]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.05, 49.06], [5.99, 4.51]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.65, 50.41], [6.77, 5.94]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.65, 50.41], [7.77, 4.94]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[6.74, 50.73], [8.72, 3.23]]}, "HQ": "X", "City": "X"}, 
{"Lat": "X", "Long": "X", "Trade_Area": {"type": "Polygon", "coordinates": [[9.65, 50.19], [9.26, 5.68]]}, "HQ": "X", "City": "X"}]
'

select *
from openjson(@data)
with 
(
  type nvarchar(100) '$.Trade_Area.type',
  coordinates nvarchar(max) '$.Trade_Area.coordinates' as json
) as t

输出

type             coordinates
---------------- -----------------------------------
Polygon          [[10.74, 50.67], [7.75, 5.21]]
Polygon          [[11.66, 49.55], [1.73, 5.05]]
Polygon          [[12.91, 50.98], [2.72, 8.29]]
Polygon          [[4.65, 50.39], [3.53, 6.29]]
Polygon          [[4.48, 50.32], [4.42, 4.79]]
Polygon          [[6.05, 49.06], [5.99, 4.51]]
Polygon          [[6.65, 50.41], [6.77, 5.94]]
Polygon          [[6.65, 50.41], [7.77, 4.94]]
Polygon          [[6.74, 50.73], [8.72, 3.23]]
Polygon          [[9.65, 50.19], [9.26, 5.68]]

【讨论】:

  • 非常感谢您的回答!
猜你喜欢
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2016-04-27
  • 2020-07-13
  • 2020-01-15
  • 1970-01-01
相关资源
最近更新 更多