使用JSON_VALUE查询数组

时间:2019-05-26 03:15:05

标签: json sql-server

我有一个包含以下JSON数据的字段

[
   {
      "@odata.type":"#Sitecore.XConnect.Goal",
      "CustomValues":[

      ],
      "DefinitionId":"82c4c49c-b6b2-4b02-8e2f-fbcba9f92fe4",
      "EngagementValue":60,
      "Id":"335c92ce-5e36-4b13-9472-4940ad66e75f",
      "Timestamp":"2019-05-07T23:53:34.4268677Z"
   }
]

我正在尝试查找字段@odata.type等于#Sitecore.XConnect.Goal的所有SQL行。但这是在数组内部。我已经在下面尝试了SQL,但无法返回任何SQL行。

SELECT *
  FROM [usms_Xdb.Collection.Shard0].[xdb_collection].[Interactions]
  WHERE JSON_VALUE([Events], '$.[0]"odata.type"') = '#Sitecore.XConnect.Goal'

1 个答案:

答案 0 :(得分:1)

您需要将路径指定为<html><body> <svg id="svg" width="100%" height="100%"></svg> <script> let svg = document.getElementById("svg"); function line(x1, y1, x2, y2) { let e = document.createElementNS(svg.namespaceURI, 'line'); e.setAttribute('x1', x1); e.setAttribute('y1', y1); e.setAttribute('x2', x2); e.setAttribute('y2', y2); e.setAttribute('style', 'stroke:#000'); svg.appendChild(e); } function frame_rect(r) { let e = document.createElementNS(svg.namespaceURI, 'rect'); e.setAttribute('x', r.x); e.setAttribute('y', r.y); e.setAttribute('width', r.width); e.setAttribute('height', r.height); e.setAttribute('style', 'stroke:#000;fill:none'); svg.appendChild(e); } onresize = function() { svg.innerHTML = ''; // remove all elements from the SVG let r = svg.getBoundingClientRect(); line(r.x,r.y,r.x+r.width,r.y+r.height); line(r.x,r.y+r.height,r.x+r.width,r.y); frame_rect(r); } onresize() </script></body></html>(缺少点运算符'$[0]."@odata.type"',它指示您的.对象的成员)。

$[0]

示例:

SELECT *
FROM [usms_Xdb.Collection.Shard0].[xdb_collection].[Interactions]
WHERE JSON_VALUE([Events], '$[0]."@odata.type"') = '#Sitecore.XConnect.Goal'

输出:

DECLARE @json nvarchar(max) = N'[
   {
      "@odata.type":"#Sitecore.XConnect.Goal",
      "CustomValues":[

      ],
      "DefinitionId":"82c4c49c-b6b2-4b02-8e2f-fbcba9f92fe4",
      "EngagementValue":60,
      "Id":"335c92ce-5e36-4b13-9472-4940ad66e75f",
      "Timestamp":"2019-05-07T23:53:34.4268677Z"
   }
]'

SELECT JSON_VALUE(@json, '$[0]."@odata.type"') AS JsonValue