JSON schema error Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined' with a Regex

Viewed 192

I have the following json but for some reason I keep getting an error with this line:

"Test": "Hostname:\s+(?<Hostname>[^\n]*)\s+Site name:\s+(?<SiteName>[^\n]*"

My JSON:

{
    "Rules": [{
        "Field": "Subject",
        "Test": "^(Azure Exception)",
        "Cases": [{
            "Field": "Content",
            "Test": "Hostname: az.....(?<Hostname>[^\n])",
            "Cases": [{
                "Field": "Content",
                "Test": "Hostname:\s+(?<Hostname>[^\n]*)\s+Site name:\s+(?<SiteName>[^\n]*)"
            }]
        }]
    }]
}

This is a valid regex expression as far as i can tell. Anyone know what I'm doing wrong?

I'm using https://jsonlint.com/ to test my json.

Testing the regex: https://regex101.com/r/l4tn4W/3/.

Cheers

1 Answers

You should use

"Test": "Hostname:\\s+(?<Hostname>.*)\\s+Site name:\\s+(?<SiteName>.*)"

The [^\n] matches any char but a newline, that can be replaced with . pattern for brevity since . matches any chars but line break chars.

Also, the backslashes must be doubled to form regex escape sequences.

Note you may have to further simplify the pattern depending on the results you get:

"Test": "Hostname:[ \t]+(.*?)[ \t]+Site name:[ \t]+(.*)"

where \s is replaced with [ \t] that matches a space or a tab, all named capturing groups are replaced with simple numbered capturing groups and the first .* is replaced with a non-greedy counterpart, .*?, that matches any zero or more chars other than line break chars, as few as possible.

Related