I have a SQL Server table with a NVARCHAR(MAX) NOT NULL column with a default of empty string. The check constraint checks if the content is valid JSON if it is not null or an empty string:
CHECK ((ISNULL(ISJSON(NULLIF([MyJSONColumn],'')), (1)) = (1)
If the column already contains some JSON, for example, the following:
["This is some original stuff"]
then I can append to it using JSON_MODIFY.
For example, this works:
UPDATE t
SET t.MyJSONColumn = JSON_MODIFY(t.MyJSONColumn, 'append $', '"This is some appended stuff."')
FROM dbo.MyTable t
WHERE t.PrimaryKey = 1
and if I then select from the table:
SELECT MyJsonColumn
FROM dbo.MyTable
WHERE PrimaryKey = 1
I get my appended JSON, like so:
["This is some original stuff","This is some appended stuff."]
All is well. However, if I attempt the exact same JSON_MODIFY update when the column is an empty string, I get this error:
JSON text is not properly formatted. Unexpected character '.' is found at position 0.
The only way I can initially set the value of a blank MyJSONColumn is to do something like this. This works, but is of course very ugly:
UPDATE t
SET t.MyJSONColumn = '["This is some stuff."]')
After I get that initial value in the column, I can then use JSON_MODIFY to append to it.
Can I use JSON_MODIFY to set that initial value? I've tried adding the "lax" modifier, but I still get the error (ie 'append lax $').