如何从多维JSON数组访问元素(在SQL Server中)

时间:2019-06-26 14:26:15

标签: arrays json sql-server multidimensional-array

我有一个多维JSON数组,我正在SQL Server中访问JSON数组,并使用'OPENJSON'将JSON数据转换为SQL。我目前在从多维数组中获取数据时遇到问题

Declare @Json nvarchar(max)

Set @Json= '[{
    "id": 0,
    "healthandSafety": "true",
    "estimationCost": "7878",
    "comments": "\"Comments\"",
    "image": [{
        "imageData": "1"
    }, {
        "imageData": "2"
    }, {
        "imageData": "3"
    }, {
        "imageData": "4"
    }, {
        "imageData": "5"
    }]
}, {
    "id": 1,
    "healthandSafety": "false",
    "estimationCost": "90",
    "comments": "\"89089\"",
    "image": [{
        "imageData": "6"
    }, {
        "imageData": "7"
    }, {
        "imageData": "8"
    }, {
        "imageData": "9"
    }, {
        "imageData": "10"
    }, {
        "imageData": "11"
    }]
}]'

Select ImageJsonFile from OPENJSON (@Json) with (ImageJsonFile nvarchar(max) '$.image[0].imageData')

当我尝试上面的代码时,我获得了以下输出:

ImageJsonFile
1
6

我期望的输出:

ImageJsonFile
1
2
3
4
5

2 个答案:

答案 0 :(得分:2)

您需要定义查询路径:

Select * from OPENJSON (@Json,'$[0].image') with (ImageJsonFile nvarchar(max) '$.imageData')

答案 1 :(得分:2)

您已经有了答案,所以这只是添加一些更多细节:

以下内容将从您的多维数组中获取所有数据,而不仅仅是您必须明确指定的一个数组索引。

DECLARE @Json NVARCHAR(MAX)=
N'[{
    "id": 0,
    "healthandSafety": "true",
    "estimationCost": "7878",
    "comments": "\"Comments\"",
    "image": [{
        "imageData": "1"
    }, {
        "imageData": "2"
    }, {
        "imageData": "3"
    }, {
        "imageData": "4"
    }, {
        "imageData": "5"
    }]
}, {
    "id": 1,
    "healthandSafety": "false",
    "estimationCost": "90",
    "comments": "\"89089\"",
    "image": [{
        "imageData": "6"
    }, {
        "imageData": "7"
    }, {
        "imageData": "8"
    }, {
        "imageData": "9"
    }, {
        "imageData": "10"
    }, {
        "imageData": "11"
    }]
}]';

-查询

SELECT A.id
      ,A.healthandSafety
      ,A.estimationCost
      ,A.comments
      ,B.imageData 
FROM OPENJSON(@Json)
WITH(id INT
    ,healthandSafety BIT
    ,estimationCost INT
    ,comments NVARCHAR(1000)
    ,[image] NVARCHAR(MAX) AS JSON ) A
CROSS APPLY OPENJSON(A.[image])
WITH(imageData INT) B;

结果

+----+-----------------+----------------+----------+-----------+
| id | healthandSafety | estimationCost | comments | imageData |
+----+-----------------+----------------+----------+-----------+
| 0  | 1               | 7878           | Comments | 1         |
+----+-----------------+----------------+----------+-----------+
| 0  | 1               | 7878           | Comments | 2         |
+----+-----------------+----------------+----------+-----------+
| 0  | 1               | 7878           | Comments | 3         |
+----+-----------------+----------------+----------+-----------+
| 0  | 1               | 7878           | Comments | 4         |
+----+-----------------+----------------+----------+-----------+
| 0  | 1               | 7878           | Comments | 5         |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 6         |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 7         |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 8         |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 9         |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 10        |
+----+-----------------+----------------+----------+-----------+
| 1  | 0               | 90             | 89089    | 11        |
+----+-----------------+----------------+----------+-----------+

简而言之:

我们使用第一个OPENJSON来获取第一级的元素。 WITH子句将命名所有元素,并以[image]返回NVARCHAR(MAX) AS JSON。这样可以使用另一个OPENJSON来读取imageData(嵌套维度)中的数字,而id列是分组键。