Advertisement
Advanced SQL Server, JSON Support in SQL Server

How would you validate that a value stored in a column actually contains well formed JSON before allowing it to be inserted or updated?

Short answer

You add a check constraint using the ISJSON function, which returns whether a given string is valid JSON, ensuring that any value inserted or updated in that column must be properly formatted JSON, catching malformed data at the database level before it can cause errors later…

Save SQL Server mock interview
Advertisement
AModel answer
You add a check constraint using the ISJSON function, which returns whether a given string is valid JSON, ensuring that any value inserted or updated in that column must be properly formatted JSON, catching malformed data at the database level before it can cause errors later when the application tries to parse it.
ALTER TABLE Users
ADD CONSTRAINT CK_ValidJSON CHECK (ISJSON(ProfileData) = 1);
Real-world example
A user profile table enforces that its flexible profile data column always contains valid JSON using a check constraint with ISJSON, preventing a bug in the application from ever storing malformed data that could break downstream processing.
Likely follow-ups
  • What does ISJSON return for an empty string or NULL value?
  • Does adding this constraint have any noticeable performance impact on writes?
Related topics