SQL 将 Json 数组解析为行

SQL Parse Json array to rows

我正在尝试将 'custinfo' 数组解析为行,而不是查询中的特定列(数组中可以有 none 或许多值)

     DECLARE @json NVARCHAR(MAX) ='{
  "customer": [
    {
      "id": "123",
      "history": [
        {
          "id": "a123",
          "dates": [
            {
              "date": "2022-03-19",
              "details": {
                "custinfo": [
                  "male",
                  "married"
                ],                
                "age": 40            
                }}]}]}]}'
                                             
SELECT
    JSON_VALUE ( j.[value], '$.id' ) AS CustId,
  JSON_VALUE ( m.[value], '$.id' ) AS CustId_Hist,
   JSON_VALUE ( a1.[value], '$.date' ) AS date,
   JSON_VALUE ( a1.[value], '$.details.age' ) AS age,
   JSON_VALUE ( a1.[value], '$.details.custinfo[0]' ) AS custinfo0,
   JSON_VALUE ( a1.[value], '$.details.custinfo[1]' ) AS custinfo1
FROM OPENJSON( @json, '$."customer"' ) j
 CROSS APPLY OPENJSON ( j.[value], '$."history"' ) AS m
  CROSS APPLY OPENJSON ( m.[value], '$."dates"' ) AS a1

期望的结果:

就像我在评论中提到的那样,我会改用 WITH 子句并定义您的列及其数据类型。然后,您还可以使用以下内容将值放入您想要的 2 个单独的行中。请注意末尾的额外 OPENJSON ,它将 custinfo 视为数组;返回 2 行(数组中每个值 1 行):

DECLARE @json NVARCHAR(MAX) ='{
  "customer": [
    {
      "id": "123",
      "history": [
        {
          "id": "a123",
          "dates": [
            {
              "date": "2022-03-19",
              "details": {
                "custinfo": [
                  "male",
                  "married"
                ],                
                "age": 40            
                }}]}]}]}';
                                             
SELECT c.id AS CustId,
       h.id AS CustId_Hist,
       d.date AS date,
       d.age AS age,
       ci.[value] AS custinfo
FROM OPENJSON( @json,'$.customer')
     WITH (id int,
           history nvarchar(MAX) AS JSON) c
     CROSS APPLY OPENJSON (c.history)
                 WITH (id varchar(10),
                       dates nvarchar(MAX) AS JSON) h
     CROSS APPLY OPENJSON (h.dates) 
                 WITH(date date,
                      details nvarchar(MAX) AS JSON,
                      age int '$.details.age') d
     CROSS APPLY OPENJSON(d.details,'$.custinfo') ci;