简体   繁体   中英

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

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 occurring multiple times. I am interested in all values AFTER the tag IncidentName .

It looks something like this within the JSON (but then nested in variable arrays that sometimes exist and sometimes don't):

"IncidentName":"Value1",
"IncidentName":"Value2",
"IncidentName":"Value3"

Could someone help me with building a loop/query that returns all of these values?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM