jsonb_set_lax()
是一个系统函数,用于在jsonb
值中,通过指定路径来替换或创建一项,并考虑NULL
值。
jsonb_set_lax()
在 PostgreSQL 13 中添加。
用法
jsonb_set_lax (target
jsonb
,
path
text
[],
new_value
jsonb
[,create_if_missing
boolean
[,null_value_treatment
text
]
]
)→jsonb
如果 new_value 不为 NULL,jsonb_set_lax()
的行为与 jsonb_set()
完全相同。
否则,其行为取决于 null_value_treatment
参数的指定方式。此参数可以设置为以下值之一:
use_json_null
(默认)delete_key
return_target
raise_exception
变更历史
- PostgreSQL 13
- 添加 (commit a83586b5)
示例
jsonb_set_lax()
的基本用法示例
postgres=# SELECT jsonb_set_lax('["foo","bar"]', '{1}', null); jsonb_set_lax --------------- ["foo", null] (1 row)
将 null_value_treatment
指定为 delete_key
postgres=# SELECT jsonb_set_lax('["foo","bar"]', '{1}', null, true, 'delete_key'); jsonb_set_lax --------------- ["foo"] (1 row)
将 null_value_treatment
指定为 return_target
postgres=# SELECT jsonb_set_lax('["foo","bar"]', '{1}', null, true, 'return_target'); jsonb_set_lax ---------------- ["foo", "bar"] (1 row)
将 null_value_treatment
指定为 raise_exception
postgres=# SELECT jsonb_set_lax('["foo","bar"]', '{1}', null, true, 'raise_exception'); ERROR: JSON value must not be null DETAIL: Exception was raised because null_value_treatment is "raise_exception". HINT: To avoid, either change the null_value_treatment argument or ensure that an SQL NULL is not passed.
为 null_value_treatment
提供无效设置
postgres=# SELECT jsonb_set_lax('["foo","bar"]', '{1}', null, true, 'eat_cheese'); ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
参考资料
- PostgreSQL 文档: JSON 处理函数