提取存储在Azure SQL / 2016中的JSON文本中的特定键值

时间:2018-08-14 02:59:47

标签: sql json

我的SQL列之一中存储了以下JSON文本:

[
    {"key":"Input Items By","value":"Text"},
    {
        "key":"ActualDeliveryLocation",
        "value":{"lat":1.3109613,"lng":103.8372307,"accuracy":10}
    }
]

通过在线参考和反复试验,我设法获得了想要的结果(如下图所示),但是应该有更简单/直接的方法吗?

select 
    c.id, lat.[key], lat.[Value]
from
    (select b.id,gpsdata.[Value] as loc 
     from
         (select 
              p.Id, P.NoteFromDriver, AttsData.[key], AttsData.[Value] as gps
          from
              Logistics.DeliveryDetail P 
          cross apply
              OPENJSON(P.NoteFromDriver) AS AttsData
          where 
              ISJSON(P.NoteFromDriver) = 1 and id = '536252') b 
    cross apply 
        OPENJSON(b.gps) as gpsdata) c 
cross apply 
    OPENJSON(c.loc) as lat 
where 
    ISJSON(c.loc) = 1

result

谢谢!

0 个答案:

没有答案