将嵌套 JSON 列解析到 SQL 服务器

问题描述 投票:0回答:2

我正在尝试解析我的 SQL 表之一中的嵌套 JSON 列。嵌套列在多个地方都有一个键值“text”。我希望这个键“文本”的所有值在一个单元格中彼此相邻。

以下是我的示例数据。

{
    "version": 1,
    "type": "doc",
    "content": [
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Please assign ticket to RCA team."
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "RCA Team"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tClient code \u2013 5374 \u12345 Apple Financial"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tDividend Task ID \u2013 12345"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPrevious MI IR Number - 12345"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Against the above task please can I have the following data:"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tHURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPartyURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tEligible Share Balance"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCash Currency Dividend Due"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tTax Deducted"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tholder name including any joint holders"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCurrent full address"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tShare balance against task"
                }
            ]
        }
    ]
}

尝试了以下代码

SELECT ID, [Key], Texts
FROM TEST_Table
CROSS APPLY OPENJSON(Description)
WITH 
(    
    Texts varchar(max) '$.content.content.text' 
) as TextJson;

但什么也没得到。需要帮助请。

sql-server
2个回答
0
投票

我不确定你为什么想要这个,但这是一个解决方案:

DECLARE @json nvarchar(max) = N'{
    "version": 1,
    "type": "doc",
    "content": [
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Please assign ticket to RCA team."
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "RCA Team"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tClient code \u2013 5374 \u12345 Apple Financial"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tDividend Task ID \u2013 12345"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPrevious MI IR Number - 12345"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Against the above task please can I have the following data:"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tHURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPartyURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tEligible Share Balance"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCash Currency Dividend Due"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tTax Deducted"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tholder name including any joint holders"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCurrent full address"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tShare balance against task"
                }
            ]
        }
    ]
}';

WITH cte AS (
    SELECT  cast(-1 AS bigint) AS parentKey, cast([key] AS bigint) AS [currentKey], value, 1 AS level
    FROM    OPENJSON(@json, '$.content') o
    UNION ALL
    SELECT  c.currentKey, cast(o.[key] AS bigint), o.value, c.level + 1
    FROM    cte c
    CROSS apply openjson(c.value, '$.content') o
)
SELECT  string_agg(json_value(value, '$.text'), ' ') within GROUP (ORDER BY [level], parentKey, currentkey)
FROM    cte
WHERE   JSON_VALUE(value, '$.type') = 'text'

我使用递归 CTE 调用每个 subjson 部分,然后将其聚合成一个长字符串,按父键和当前键排序。

我使用“ ”作为段落分隔符,但如果您有一些特定需求,您可以使用任何您想要的东西


0
投票

您可以使用递归 CTE 来提取并解析每个节点中的

content
属性:

with cte as(
    select  ID,type,text,content 
    from @table t cross apply openjson(t.Description) with (
        type nvarchar(max),
        text nvarchar(max),
        content nvarchar(max) as json)  
    as x
    union all
    select c.ID,u.type,u.text,u.content 
    from cte as c cross apply Openjson(c.content) with (
        type nvarchar(max),
        text nvarchar(max),
        content nvarchar(max) as json) as u
)
select id,STRING_AGG(text,'\n')  
from cte
where type='text'
group by ID

使用此测试 JSON

declare @json nvarchar(max)=N'{
    "version": 1,
    "type": "doc",
    "content": [
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Please assign ticket to RCA team."
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "RCA Team"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tClient code \u2013 5374 \u12345 Apple Financial"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tDividend Task ID \u2013 12345"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPrevious MI IR Number - 12345"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Against the above task please can I have the following data:"
                }
            ]
        },
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "-\tHURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tPartyURN"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tEligible Share Balance"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCash Currency Dividend Due"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tTax Deducted"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tholder name including any joint holders"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tCurrent full address"
                },
                {
                    "type": "hardBreak"
                },
                {
                    "type": "text",
                    "text": "-\tShare balance against task"
                }
            ]
        }
    ]
}';

还有桌子

declare @table table(ID int primary key,Description nvarchar(max))
insert into @table (ID,Description) 
VALUES (1,@json),
(2,@json)

这会产生:

id  Texts
1   - HURN\n- PartyURN\n- Eligible Share Balance\n- Cash Currency Dividend Due\n- Tax Deducted\n- holder name including any joint holders\n- Current full address\n- Share balance against task\nAgainst the above task please can I have the following data:\n- Client code – 5374 ሴ5 Apple Financial\n- Dividend Task ID – 12345\n- Previous MI IR Number - 12345\nRCA Team\nPlease assign ticket to RCA team.
2   - HURN\n- PartyURN\n- Eligible Share Balance\n- Cash Currency Dividend Due\n- Tax Deducted\n- holder name including any joint holders\n- Current full address\n- Share balance against task\nAgainst the above task please can I have the following data:\n- Client code – 5374 ሴ5 Apple Financial\n- Dividend Task ID – 12345\n- Previous MI IR Number - 12345\nRCA Team\nPlease assign ticket to RCA team.
© www.soinside.com 2019 - 2024. All rights reserved.