jsonb_set_lax()

用于修改jsonb值中某一项的函数

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

变更历史

示例

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"

分类

JSON, 系统函数

另请参阅

jsonb_set(), jsonb_strip_nulls()

反馈

提交关于 "jsonb_set_lax()" 的任何评论、建议或更正 在此处