【问题标题】:Query against JSON array elements?查询 JSON 数组元素?
【发布时间】:2016-06-23 20:20:27
【问题描述】:

是否可以对数组元素运行查询?似乎它在顶级项目上工作正常......假设我有这个 json:

{
  "_id": "5769cfbf7e45b52e388bbc78",
  "address": {
    "street": "2 Avenue",
    "zipcode": "10075",
    "building": "1480",
    "coord": [
      73.9557413,
      40.7720266
    ]
  },
  "borough": "Manhattan",
  "cuisine": "Italian",
  "grades": [
    {
      "date": "2014-10-01T00:00:00.000Z",
      "grade": "A",
      "score": 11
    },
    {
      "date": "2014-01-06T00:00:00.000Z",
      "grade": "B",
      "score": 17
    }
  ],
  "name": "Vella",
  "restaurant_id": "41704620"
}

是否可以使用 JSON_QUERY / JSON_VALUE 对 Grades[xxx].score 执行 where 子句? IE。我想退回所有 Grades.score 为 >= 17 的文件。

【问题讨论】:

  • 我想你想使用OPENJSON。我没有可用的 SQL Server 2016,所以我真的无能为力。

标签: sql json sql-server-2016


【解决方案1】:

SQL Azure 和 SQL Server 2016 现在原生支持 JSON。您可以在 'EXISTS' 子句中使用以下查询,或者任何需要的查询(假设 @json 包含给定的 JSON 数据):

SELECT g.score as score
FROM OPENJSON(@json) WITH (Id varchar(max)) j
    CROSS APPLY OPENJSON(JSON_QUERY(@json, '$.grades')) WITH (score int) AS g
WHERE g.score = 17

【讨论】:

  • SQL Azure 和 SQL Server 2016 原生支持 JSON。
猜你喜欢
  • 2013-11-03
  • 2021-11-24
  • 2014-05-09
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 2017-04-26
  • 2020-12-10
  • 2015-07-10
相关资源
最近更新 更多