- Categories:
Semi-structured and structured data functions (Array/Object)
OBJECT_ CONSTRUCT_ KEEP_ NULL_ STRUCTURED¶
Returns a structured OBJECT constructed from the arguments that retains key-value pairs with NULL values.
Unlike OBJECT_CONSTRUCT_KEEP_NULL, which returns a semi-structured OBJECT, this function
always returns a structured OBJECT(<key> <type>, ...) value.
- See also:
Syntax¶
Arguments¶
keyThe key in a key-value pair. Each key must be a non-empty constant VARCHAR value.
valueThe value that is associated with the key. The value can be any data type.
*When invoked with an asterisk (wildcard), the OBJECT value is constructed from the specified data using the attribute names as keys and the associated values as values. See the examples below.
When you pass a wildcard to the function, you can qualify the wildcard with the name or alias for the table. For example, to pass in all of the columns from the table named
mytable, specify the following:You can also use the ILIKE and EXCLUDE keywords for filtering:
-
ILIKE filters for column names that match the specified pattern. Only one pattern is allowed. For example:
-
EXCLUDE filters out column names that don’t match the specified column or columns. For example:
Qualifiers are valid when you use these keywords. The following example uses the ILIKE keyword to filter for all of the columns that match the pattern
col1%in the tablemytable:The ILIKE and EXCLUDE keywords can’t be combined in a single function call.
For this function, the ILIKE and EXCLUDE keywords are valid only in a SELECT list or GROUP BY clause.
For more information about the ILIKE and EXCLUDE keywords, see the “Parameters” section in SELECT.
-
Returns¶
Returns a structured OBJECT whose fields are the specified keys and whose field types are the types of the associated values.
Usage notes¶
- You must specify an even number of arguments (one or more key-value pairs). Specifying an odd number of arguments returns an error.
- Each key must be a non-empty constant VARCHAR value. A key that is NULL, empty, not a constant, or not a VARCHAR value returns an error.
- The keys must be unique. Specifying a duplicate key returns an error.
- If a value is NULL, the key-value pair is kept. A value that is an untyped NULL literal returns an error because
the field type can’t be determined. To keep a NULL value, cast the value to a specific type (for example,
NULL::VARCHAR). - This function always returns a structured OBJECT, even when none of the input values are structured types.
- To construct a semi-structured OBJECT that retains key-value pairs with NULL values, use OBJECT_CONSTRUCT_KEEP_NULL.
Examples¶
Construct a structured object, retaining the key-value pair with a NULL value:
Use the wildcard character (*) to construct a structured object from the columns of a table: