将 json 值更新为 null

you*_*gme 4 sql-server t-sql json

据我了解,如果您选择json_valuejson_query指定 json 文档中不存在的键或对象,那么在严格模式下,您将收到错误。例如,这使您可以确认文档中是否指定了该密钥。

如果文档中包含示例键值对:

"Test":null
Run Code Online (Sandbox Code Playgroud)

...在严格模式下,这将返回“NULL”。换句话说,您现在知道该键已在文档中定义,并且其值为 null。

现在想象您的文档包含:

 "Test":"Some string"
Run Code Online (Sandbox Code Playgroud)

而你查询:

 select json_modify(@json, '$.Test', null);
Run Code Online (Sandbox Code Playgroud)

这将返回一个缺少“Test”键的 json 字符串。

如何正确地将键值设置为 null,而不是将其从 json 文档中删除?

(上面的示例可能看起来很荒谬,但我可以想象在将 json 文档传递到下一个查询或系统之前将值设置为 null,并要求文档中存在“Test”键。)

McN*_*ets 8

根据 MS 文档关于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        |
+----------------+-------------+-----------------------------------------------------------------------------------------+---------------------------------+
Run Code Online (Sandbox Code Playgroud)

只需使用文档提供的示例,在 json 路径前面添加关键字 strict:

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

回报

{"name":null,"skills":["C#","SQL"]}
Run Code Online (Sandbox Code Playgroud)

db<>在这里摆弄

  • 只是添加:如果你想添加一个不存在的新字段,值为空,只需使用 JSON_MODIFY 两次 `JSON_MODIFY(JSON_MODIFY(@info,'$.name',''),'strict $.name',空)`。一次没有严格,然后有严格。 (2认同)