Advertisement

Latest Post

Thursday, 27 July 2017

What is ISJSON Function in Sql Server? When and Why we use this?

ISJSON Function: It is a validation function that check string is formed JSON or not. It checks the input string is JSON or not. It returns Boolean value.  
If the input string is valid JOSN format, it will return 1 otherwise it will return 0.


SYNTAXISJSON(input_string )                                                 
Where input_string can be type of, VARCHAR, NVARCHAR.

Example 1:  ISJSON function will return value as 1 if Input is a valid JSON.

DECLARE @inputJSON_string NVarchar(Max) = '[{"ENAME": "ALOK","JOB": "ADMIN"}]'
IF ISJSON ( @inputJSON_string ) = 1
    PRINT 'Valid JSON'

OUTPUT:-
Valid JSON

Example 2:  ISJSON function will return value as 0 if Input is an Invalid JSON.

DECLARE @inputJSON_string NVarchar(Max) = 'alok singh'

IF ISJSON ( @inputJSON_string ) = 0
   PRINT 'InValid JSON'

OUTPUT:-
InValid JSON

Remark: - Input is a null value, and then ISJSON function will return 0.

DECLARE @inputJSON_string NVarchar(Max) = ''

IF ISJSON ( @inputJSON_string ) = 1
   PRINT 'Valid JSON'
IF ISJSON ( @inputJSON_string ) = 0
   PRINT 'InValid JSON'

OUTPUT:-
InValid JSON

Government Jobs