patternsqlMinor
update json value to null
Viewed 0 times
jsonnullupdatevalue
Problem
As I understand it, if you select
If an example key-value pair is included in the document thus:
... 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:
And you query:
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.)
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.)
Solution
According to MS Docs about JSON_MODIFY:
Simply add the keyword strict in front of the json path, using the example supplied by the docs:
returns
db<>fiddle here
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| 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
Code Snippets
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
| 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 |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+DECLARE @info NVARCHAR(100)='{"name":"John","skills":["C#","SQL"]}';
SET @info=JSON_MODIFY(@info,'strict $.name',NULL):
PRINT @info;{"name":null,"skills":["C#","SQL"]}Context
StackExchange Database Administrators Q#235006, answer score: 8
Revisions (0)
No revisions yet.