【问题标题】:How can i access the elements from multi dimensional json array (in SQL Server)我如何访问多维 json 数组中的元素(在 SQL Server 中)
【发布时间】:2019-06-26 14:26:15
【问题描述】:

我有一个多维 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

【问题讨论】:

    标签: arrays json sql-server multidimensional-array


    【解决方案1】:

    您需要定义查询路径:

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

    【讨论】:

    • 你就是这样做的!谢谢 Piotr,我在这里学到了一些东西。 :)
    【解决方案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-column 是分组键。

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 2015-07-05
      • 2017-10-28
      • 1970-01-01
      • 2014-06-07
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多