简体   繁体   English

查询JSON数组元素?

[英]Query against JSON array elements?

Is it possible to run queries on array elements? 是否可以对数组元素运行查询? Seems like it works ok on the top level items... Lets say I have this json: 似乎在顶级项目上可以正常工作...让我们说这个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"
}

Is it possible to use JSON_QUERY / JSON_VALUE to do a where clause against grades[xxx].score? 是否可以使用JSON_QUERY / JSON_VALUE对grades [xxx] .score执行where子句? Ie I want to return all documents where ANY of the grades.score is >= 17. 即我想返回所有grades.score> = 17的所有文档。

SQL Azure and SQL Server 2016 supports JSON natively now. SQL Azure和SQL Server 2016现在本地支持JSON。 You can use the following query in ' EXISTS ' clause, or whatever is needed (assuming @json contains the given JSON data): 您可以在' 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

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

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