JSON_OBJECT_KEEP_NULL (Lakehouse v1)
Creates a JSON object with keys and values.
- The arguments are zero or more key-value pairs(where keys are strings, and values are of any type).
- If a key is NULL, the key-value pair is omitted from the resulting object. However, if a value is NULL, the key-value pair will be kept.
- The keys must be distinct from each other, and their order in the resulting JSON might be different from the order you specify.
TRY_JSON_OBJECT_KEEP_NULLreturns a NULL value if an error occurs when building the object.
See also: JSON_OBJECT
SQL Syntax
Section titled “SQL Syntax”JSON_OBJECT_KEEP_NULL(key1, value1[, key2, value2[, ...]])
TRY_JSON_OBJECT_KEEP_NULL(key1, value1[, key2, value2[, ...]])Return Type
Section titled “Return Type”JSON object.
SQL Examples
Section titled “SQL Examples”SELECT JSON_OBJECT_KEEP_NULL();┌─────────────────────────┐│ json_object_keep_null() │├─────────────────────────┤│ {} │└─────────────────────────┘
SELECT JSON_OBJECT_KEEP_NULL('a', 3.14, 'b', 'xx', 'c', NULL);┌────────────────────────────────────────────────────────┐│ json_object_keep_null('a', 3.14, 'b', 'xx', 'c', null) │├────────────────────────────────────────────────────────┤│ {"a":3.14,"b":"xx","c":null} │└────────────────────────────────────────────────────────┘
SELECT JSON_OBJECT_KEEP_NULL('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery']);┌────────────────────────────────────────────────────────────────────────────────────────────────────┐│ json_object_keep_null('fruits', ['apple', 'banana', 'orange'], 'vegetables', ['carrot', 'celery']) │├────────────────────────────────────────────────────────────────────────────────────────────────────┤│ {"fruits":["apple","banana","orange"],"vegetables":["carrot","celery"]} │└────────────────────────────────────────────────────────────────────────────────────────────────────┘
SELECT JSON_OBJECT_KEEP_NULL('key'); |1 | SELECT JSON_OBJECT_KEEP_NULL('key') | ^^^^^^^^^^^^^^^^^^ The number of keys and values must be equal while evaluating function `json_object_keep_null('key')`
SELECT TRY_JSON_OBJECT_KEEP_NULL('key');┌──────────────────────────────────┐│ try_json_object_keep_null('key') │├──────────────────────────────────┤│ NULL │└──────────────────────────────────┘