解析包含多个数组和键的 JSON 列

Parse JSON Column containing multiple arrays and keys

我经历了几次 examples/tutorials 尝试使用 SQL Server (2017) 中的 JSON 函数解析包含多个数组的 JSON 列,但是我就尝试解析以下 JSON 格式而言,我仍然很短,如下所示。

出于示例目的 - 我将在下面的代码示例中将其声明为 @json 变量。我已经尝试了多次迭代来尝试提取这些值,所以这是我在尝试解析下面显示的示例时停止的地方。

DECLARE @json nvarchar(max)
SET @json = N'[
  [
    {
      "Title": "Title 1",
      "Property": "String1",
      "Value": "123456",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    },
    {
      "Title": "Title 2",
      "Property": "String2",
      "Value": "54321",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    }
  ],
  [
    {
      "Title": "Title 3",
      "Property": "String3",
      "Value": "33333333",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    },
    {
      "Title": "Title 4",
      "Property": "String4",
      "Value": "44444444",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    }
  ]
]'
SELECT JSON_VALUE(j.value, '$.Title') AS 'Title Output', j.[key], j.Value, JSON_VALUE(j.value, '$.Title[0]') AS Title1
--STRING_AGG('Value: ' + v.[value] + ' Title: ' + t.[value], ', ') AS [Values]
FROM OPENJSON(@json) j

根据 JSON 格式如何存储到列中,我希望获得所有标题和值,如下所示。

预期输出列

标题 1 - 123456,标题 2 - 54321,标题 3 - 33333333,标题 4 - 44444444

我还尝试在解析数据之前更改 JSON 字符串,例如使用数组名称 pre-pending 以帮助识别 collection 成员的位置,但是我只获得了第一个Title & Value

它包含一个数组 [] 和对象 {}

您可以CROSS APPLY数组的值来获取元素。

这是一个使用 table.
的示例 但是变量的原理是一样的。

CREATE TABLE test
(
  id int identity(1,1) primary key,
  jsonCol nvarchar(max)
);

INSERT INTO test VALUES
(N'[
  [
    {
      "Title": "Title 1",
      "Property": "String1",
      "Value": "123456",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    },
    {
      "Title": "Title 2",
      "Property": "String2",
      "Value": "54321",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    }
  ],
  [
    {
      "Title": "Title 3",
      "Property": "String3",
      "Value": "33333333",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    },
    {
      "Title": "Title 4",
      "Property": "String4",
      "Value": "44444444",
      "ValueArray": [
        ""
      ],
      "SecondaryValue": null,
      "SelectedItem": null
    }
  ]
]');

查询:

SELECT id, a.[Values]
FROM test t
OUTER APPLY
(
   SELECT
   STRING_AGG(CONCAT(
      JSON_VALUE(obj.value,'$.Title'),
      ' - ', 
      JSON_VALUE(obj.value,'$.Value')
   ), ', ') AS [Values]
   FROM OPENJSON(t.jsonCol) AS arr
   CROSS APPLY OPENJSON(arr.Value) AS obj
) a;

结果:

id | Values                                                                   
-: | :------------------------------------------------------------------------
 1 | Title 1 - 123456, Title 2 - 54321, Title 3 - 33333333, Title 4 - 44444444

db<>fiddle here

上测试