简体   繁体   English

查询JSON字符串SQL Server 2016

[英]Query a json string SQL Server 2016

I have the hardest time querying a json string. 我最难的时候查询一个json字符串。 This is my string: 这是我的字符串:

{
   "Microsoft.Resources":{
      "resourceUri":"/subscriptions/123subscription123/resourceGroups/123resourcegroup123/providers/Microsoft.Storage/storageAccounts/123storagaccount123",
      "location":"euwest"
   }
}

I just need the following json fields into columns: 我只需要将以下json字段放入列中:

  • resourceUri resourceUri
  • Location 位置

I have tried the following query: 我尝试了以下查询:

SELECT JSON_VALUE(I.value, '$.resourceUri') as resourceUri
,      JSON_VALUE(C.value, '$.Location') as Location
FROM   OPENJSON(InstanceData, N'lax $.Microsoft.Resources') I
CROSS 
APPLY  OPENJSON(I.value, N'lax $.fields.resourceUri') C

But I get no result. 但是我没有结果。

Could someone help me with this? 有人可以帮我吗?

You're having a hard time because the name Microsoft.Resources contains a period and thus must be quoted in order to be used in paths, but beyond that it's nothing complicated: 您遇到了麻烦,因为名称Microsoft.Resources包含一个句点,因此必须使用引号将其括起来才能在路径中使用,但除此之外,它并不复杂:

DECLARE @json NVARCHAR(MAX) = N'{
   "Microsoft.Resources":{
      "resourceUri":"/subscriptions/123subscription123/resourceGroups/123resourcegroup123/providers/Microsoft.Storage/storageAccounts/123storagaccount123",
      "location":"euwest"
   }
}'

SELECT *
FROM OPENJSON(@json, '$."Microsoft.Resources"')
WITH (
    [resourceUri] NVARCHAR(MAX),
    [location] NVARCHAR(MAX)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM