SQL Server : get all string occurences (tags) from nvarchar(max) variable containing a json string

后端 未结 1 456
说谎
说谎 2020-12-22 05:12

I have a variable that contains a json string which is quite long. Datatype therefore is NVARCHAR(MAX). Within this json string there are many of the same tags

相关标签:
1条回答
  • 2020-12-22 05:36

    You can try to create SQL CLR function and using regex expression to match the data you need. Here you can find details of what is SQL CLR integration, examples of regex match function provided by Mircosoft and instructions how to create such functions.

    So, having the above setup, you can do something like this:

    DECLARE @data NVARCHAR(MAX) = N'
    {
        "glossary": {
            "title": "example glossary",
            "GlossDiv": {
                "title": "S",
                "GlossList": {
                    "GlossEntry": {
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "GlossTerm": "Standard Generalized Markup Language",
                        "Acronym": "SGML",
                        "IncidentName":"Value1",
                        "IncidentName":"Value2",
                        "Abbrev": "ISO 8879:1986",
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "IncidentName":"Value3",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossSee": "markup"
                    }
                },
            "IncidentName":"Value4"
            }
        }
    }';
    
    SELECT *
    FROM [dbo].[fn_Utils_RegexMatches] (@data, '(?i)(?<="IncidentName":")[^"]+(?=")');
    

    The regex I am using is doing the following:

    • (?i) - case insensitive search
    • (?<="IncidentName":") - searching for this pattern, but this pattern is excluded from the final capture value (Zero-width positive lookbehind assertion)
    • [^"]+ - matching every character different form "
    • (?=") - using Zero-width positive lookahead assertion to ensure the match value is enclosed by "

    Basically, you can use this quick reference to help you build regular expression that is going to work for your case.

    The hard part is understanding what SQL CLR is and to implement the Microsoft's String Utility Pack. Then, you can solve issues, which are very difficult or not efficient to solve using plain T-SQL.

    0 讨论(0)
提交回复
热议问题