SQL SERVER – Understanding JSON NULL Value Using STRICT Keyword

No Comments

Playing around with the new capability of SQL Server 2016 has been a fun journey. Even after a year, the discovery keeps happening every single day that I work with it. When I wrote the post around NULL values getting returned, lesser did I know there have been folks who work on this feature.  Let us learn in this blog post about JSON NULL Value Using STRICT Keyword. Do make sure to read the other blog too: SQL SERVER – Understanding JSON Use is Case-Sensitive

Some of the answers provided got me interested in posting the next in the series. This was a learning for me too. While using JSON, there is a keyword that can-do magic, it is called as STRICT. The same code which was earlier used in the blog has been modified to use this keyword. The same code would look like:

	"Name": "Pinal",
	"Surname": "Kumar",
	"Birth": {"DOB":"2000-12-12" , "Town":"Gandhi Nagar", "Country":"India"}
SELECT JSON_VALUE(@myjson, 'strict $.name')

Msg 13608, Level 16, State 5, Line 1
Property cannot be found on the specified JSON path.

As you can we no longer get the NULL value as discussed before. We are getting an explicit error that represents the actual problem.

While I was trying to play around with the NULL value, I figured out when the value returned string is larger than VARCHAR (4000), then there would be an error while using the STRICT keyword.

Msg 13625, Level 16, State 1, Line 1
String value in the specified JSON path would be truncated.

The learning never stops working with SQL Server. Do let me know how you are using JSON datatypes? Let us know via comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Exit mobile version