📣
TiDB Cloud Premium 开放公测中。为企业级工作负载提供无限扩展、即时弹性伸缩和高级安全保障。此页面由 AI 自动翻译,英文原文请见此处。

OBJECT_CONSTRUCT_KEEP_NULL



创建包含键和值的 JSON 对象。

  • 参数是零个或多个键值对(其中键为字符串,值可以是任意类型)。
  • 如果键为 NULL,则结果对象中会省略该键值对。但是,如果值为 NULL,则会保留该键值对。
  • 键之间必须互不相同,并且结果 JSON 中键的顺序可能与您指定的顺序不同。
  • TRY_OBJECT_CONSTRUCT_KEEP_NULL 在构建对象时如果发生错误,会返回 NULL 值。

别名

  • JSON_OBJECT_KEEP_NULL
  • TRY_JSON_OBJECT_KEEP_NULL

另请参阅:OBJECT_CONSTRUCT

语法

OBJECT_CONSTRUCT_KEEP_NULL(key1, value1[, key2, value2[, ...]]) TRY_OBJECT_CONSTRUCT_KEEP_NULL(key1, value1[, key2, value2[, ...]])

返回类型

JSON 对象。

示例

SELECT OBJECT_CONSTRUCT_KEEP_NULL(); ┌──────────────────────────────┐ │ object_construct_keep_null() │ ├──────────────────────────────┤ │ {} │ └──────────────────────────────┘ SELECT OBJECT_CONSTRUCT_KEEP_NULL('a', 3.14, 'b', 'xx', 'c', NULL); ┌───────────────────────────────────────────────────────────┐ │ object_construct_keep_null('a', 3.14, 'b', 'xx', 'c', null) │ ├───────────────────────────────────────────────────────────┤ │ {"a":3.14,"b":"xx","c":null} │ └───────────────────────────────────────────────────────────┘ SELECT OBJECT_CONSTRUCT_KEEP_NULL('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery']); ┌───────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ object_construct_keep_null('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery']) │ ├───────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ {"fruits":["apple","banana","orange"],"vegetables":["carrot","celery"]} │ └───────────────────────────────────────────────────────────────────────────────────────────────────────┘ SELECT OBJECT_CONSTRUCT_KEEP_NULL('key'); | 1 | SELECT OBJECT_CONSTRUCT_KEEP_NULL('key') | ^^^^^^^^^^^^^^^^^^ The number of keys and values must be equal while evaluating function `object_construct_keep_null('key')` SELECT TRY_OBJECT_CONSTRUCT_KEEP_NULL('key'); ┌─────────────────────────────────────┐ │ try_object_construct_keep_null('key') │ ├─────────────────────────────────────┤ │ NULL │ └─────────────────────────────────────┘

文档内容是否有帮助?