- Categories:
Semi-structured and structured data functions (Array/Object)
OBJECT_INSERT¶
Returns an OBJECT value consisting of the input OBJECT value with a new key-value pair inserted (or an existing key updated with a new value).
Syntax¶
Arguments¶
Required:
objectThe source OBJECT value into which the new key-value pair is inserted or in which an existing key-value pair is updated.
keyThe new key to be inserted into the OBJECT value or an existing key whose value is being updated. The specified key must be different from all existing keys in the OBJECT value, unless
updateFlagis set to TRUE.valueThe value associated with the key.
Optional:
updateFlagA Boolean flag that, when set to TRUE, specifies that the input value updates the value of an existing key in the OBJECT value, rather than inserting a new key-value pair.
The default is FALSE.
Returns¶
This function returns a value that has the OBJECT data type.
Usage notes¶
The function supports JSON null values, but not SQL NULL values or keys:
If
keyis any string other than NULL andvalueis a JSON null (for example,PARSE_JSON('null')), the key-value pair is inserted into the returned OBJECT value.If either
keyorvalueis a SQL NULL, the key-value pair is omitted from the returned OBJECT value.
If the optional
updateFlagargument is set to TRUE, the existing inputkeyis updated to the inputvalue. IfupdateFlagis omitted or set to FALSE, calling this function with an input key that already exists in the OBJECT value results in an error.If the update flag is set to TRUE, but the corresponding key doesn’t already exist in the OBJECT value, then the key-value pair is added.
-
For the arguments that are keys, you must specify constants.
When the
updateFlagargument is FALSE (when you are inserting a new key-value pair):If you specify a key that already exists in the OBJECT value, an error occurs.
The function returns a structured OBJECT value. The type of the OBJECT value includes the newly inserted key. For example, suppose that you add the
zipcodekey with the VARCHAR value94402:The type of the inserted value determines the type added to the OBJECT type definition. In this case, the value for
zipcodeis a value cast to a VARCHAR, so the type ofzipcodeis VARCHAR.
When the
updateFlagargument is TRUE (when you are replacing an existing key-value pair):If you specify a key that doesn’t exist in the OBJECT value, an error occurs.
The function returns a structured OBJECT value of the same type.
The type of the inserted value is coerced to the type of the existing key.
Examples¶
The following examples call the OBJECT_INSERT function:
Add and update key-value pairs¶
The examples use the following table:
Add a new key-value pair to an OBJECT value¶
Insert a third key-value pair into an OBJECT value that has two key-value pairs:
Insert two new key-value pairs into the OBJECT value, while omitting one key-value pair:
dconsists of a JSON null value.
econsists of a SQL NULL value and is, therefore, omitted.
fconsists of a string containing “null”.
Update a key-value pair in an OBJECT value¶
Update an existing key-value pair ("b": "value2") in the OBJECT value with a new value ("valuex"):
Add and update nested OBJECT values¶
The examples use the following table with nested OBJECT values:
Add new nested key-value pairs to the nested OBJECT values¶
The following example adds new nested key-value pairs to the nested OBJECT values in the table. It uses a CASE expression to specify the added key-value pair for each row:
Update key-value pairs in the nested OBJECT values¶
The following example updates nested key-value pairs in the OBJECT values in the table: