【发布时间】: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