跳到主要内容

OBJECT_CONSTRUCT

Introduced or updated: v1.2.762

使用键和值创建一个 JSON 对象 (JSON Object)。

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

别名

  • JSON_OBJECT
  • TRY_JSON_OBJECT

另请参阅:OBJECT_CONSTRUCT_KEEP_NULL

语法

OBJECT_CONSTRUCT(key1, value1[, key2, value2[, ...]])

TRY_OBJECT_CONSTRUCT(key1, value1[, key2, value2[, ...]])

返回类型

JSON 对象。

示例

SELECT OBJECT_CONSTRUCT();
┌────────────────┐
│ object_construct()
├────────────────┤
│ {} │
└────────────────┘

SELECT OBJECT_CONSTRUCT('a', 3.14, 'b', 'xx', 'c', NULL);
┌──────────────────────────────────────────────┐
│ object_construct('a', 3.14, 'b', 'xx', 'c', null)
├──────────────────────────────────────────────┤
│ {"a":3.14,"b":"xx"} │
└──────────────────────────────────────────────┘

SELECT OBJECT_CONSTRUCT('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery']);
┌──────────────────────────────────────────────────────────────────────────────────────────┐
│ object_construct('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery'])
├──────────────────────────────────────────────────────────────────────────────────────────┤
│ {"fruits":["apple","banana","orange"],"vegetables":["carrot","celery"]} │
└──────────────────────────────────────────────────────────────────────────────────────────┘

SELECT OBJECT_CONSTRUCT('key');
|
1 | SELECT OBJECT_CONSTRUCT('key')
| ^^^^^^^^^^^^^^^^^^ The number of keys and values must be equal while evaluating function `object_construct('key')`


SELECT TRY_OBJECT_CONSTRUCT('key');
┌───────────────────────────┐
│ try_object_construct('key')
├───────────────────────────┤
NULL
└───────────────────────────┘
欢迎体验 Databend Cloud

基于 Rust + 对象存储构建的新一代多模态数仓,一个平台即可进行 BI、向量、全文检索及地理空间分析。

支持标准 SQL,自动弹性伸缩,助您快速构建现代化数据平台。

注册即领 ¥200 代金券。

注册体验