Skip to main content

JSON_INSERT

Inserts new values into a JSON document at specified paths without overwriting existing values.

See also:
[SELECT] [JSON_EXTRACT] [JSON_SET] [JSON_REPLACE]


Syntax

SELECT JSON_INSERT(json_column, path, new_value) FROM <multipartIdentifier>;

Arguments

json_column
The JSON column or expression to be modified.

path
The JSON path expression where the new value should be inserted. If the path already exists, the original value at that path is not replaced.

new_value
The value to insert at the specified path in the JSON document.

Return Value(s)

Returns the JSON document with the new value inserted if the specified path does not already exist; otherwise, the JSON document is returned unchanged.

Behavior If a Key Exists
FunctionOverwrite if already exists?Create if does not exist?
json_insert()NoYes
json_replace()YesNo
json_set()YesYes

Examples

Insert a new key-value pair into a JSON object

-- Example: Insert a new 'region' key with the value 'Europe' into a JSON object
SELECT JSON_INSERT(json_data, '$.region', 'Europe') as updated_object;

/* expected output...
|-------------------------------------------------------------|
| updated_json |
|-------------------------------------------------------------|
| {"name":"ResourceOne", "type":"Storage", "region":"Europe"} |
|-------------------------------------------------------------|
*/

This query inserts a new key called 'region' with the value 'Europe' into the JSON document if the 'region' key does not already exist. If 'region' is already a key in the document, the original document is returned without changes.