SQL Server – How to Update JSON Value to Null

jsonsql servert-sql

As I understand it, if you select json_value or json_query specifying a key or object that does not exist in your json document, then in strict mode, you will receive an error. This allows you, for example, to confirm whether the key was specified in the document.

If an example key-value pair is included in the document thus:

"Test":null

… this will, in strict mode, return 'NULL'. In other words, you now know the key is defined in the document and its value is null.

Now imagine your document contains:

 "Test":"Some string"

And you query:

 select json_modify(@json, '$.Test', null);

This will return a json string lacking the "Test" key.

How do you properly set a key value to null, as opposed to removing it from the json document?

(The above example may seem nonsensical, but I can imagine setting a value to null before passing the json document on to the next query or system, and requiring that your "Test" key exist in the document.)

Best Answer

According to MS Docs about JSON_MODIFY:

+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| Existing value | Path exists | Lax mode                                                                                | Strict mode                     |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| Not NULL       | Yes         | Update the existing value.                                                              | Update the existing value.      |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| Not NULL       | No          | Try to create a new key:value pair on the specified path.This may fail.                 | Error - INVALID_PROPERTY        |
|                |             | For example, if you specify the path $.user.setting.theme, JSON_MODIFY does not insert  |                                 |
|                |             | the key theme if the $.user or $.user.settings objects do not exist, or if settings     |                                 |
|                |             | is an array or a scalar value.                                                          |                                 |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| NULL           | Yes         | Delete the existing property.                                                           | Set the existing value to null. |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| NULL           | No          | No action. The first argument is returned as the result.                                | Error - INVALID_PROPERTY        |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+

Simply add the keyword strict in front of the json path, using the example supplied by the docs:

DECLARE @info NVARCHAR(100)='{"name":"John","skills":["C#","SQL"]}';
SET @info=JSON_MODIFY(@info,'strict $.name',NULL):
PRINT @info;

returns

{"name":null,"skills":["C#","SQL"]}

db<>fiddle here