Validating JSON in SQL Server 2016

SQL Server 2016 and upwards has a useful feature where JSON data can be validated through SQL Code.

There are two ways of doing this:

Inline in a SQL Function/Procedure

IF (ISJSON(@json) = 1)
BEGIN
   PRINT 'Is valid JSON'
END
As part of a check constraint on a table
CREATE TABLE Person 
 ( ID INT IDENTITY PRIMARY KEY,
 json NVARCHAR(MAX)
 CONSTRAINT [Content should be formatted as JSON]
 CHECK ( ISJSON(json )>0 )
 )
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s