I have RegEx details table and I want to make array object. I am trying to make JSON using JSON_QUERY()
then I getting error like:
"JSON text is not properly formatted. Unexpected character ‘"’ is
found".
It’s only getting when I use RegEx pattern (which has slash) as JSON property value.
DECLARE @RegExData AS TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, label VARCHAR(100), ValidationName VARCHAR(100), ValidationPattern VARCHAR(100))
INSERT @RegExData(label, ValidationName, ValidationPattern) VALUES('LABEL1', 'NumberOnlyEXP','/^[0-9s]*$/')
INSERT @RegExData(label, ValidationName, ValidationPattern) VALUES('LABEL1', 'DecimalEXP','/^(d+(.d+)?)$/')
INSERT @RegExData(label, ValidationName, ValidationPattern) VALUES('LABEL2', 'DecimalWithZeroEXP','/^(d+(.d+)?)$/')
INSERT @RegExData(label, ValidationName, ValidationPattern) VALUES('LABEL2', 'AlphabetOnlyEXP',' /^[a-zA-Z]*$/')
SELECT * FROM @RegExData
2
Answers
I tried a new code, but there is a double (backslash) issue in RegEx pattern. Here is sample data and code with output.
OUTPUT
If I understand the question correctly, you may try to generate the expected output with the following statement:
In this case, if the source data contains special characters, the
FOR JSON
clause escapes them in the JSON output with. The result from the statement execution is: