Syntax
Parameters
-
json_object_expr: the expression that represents the JSON object. The object can be a JSON column, or a JSON object that is produced by a JSON constructor function such as PARSE_JSON. -
json_path: the path to the element in the JSON object that you want to insert or update. The value must be a string. For information about the JSON path syntax that is supported by the database, see Overview of JSON functions and operators. -
value: the value to be inserted or updated at the specified path. It can be a string, number, boolean, null, or a JSON object.
Return value
Returns the modified JSON document.
- Returns NULL if any argument is NULL.
- If the path exists in the JSON document, the existing value is updated (replaced).
- If the path does not exist, the new value is inserted (Upsert behavior).
- Arguments are evaluated from left to right. The result of the first path-value pair becomes the input for the second pair.
Examples
Example 1: Insert a new key into a JSON object.null value, use parse_json('null'). Passing a raw SQL NULL will return NULL for the entire result.
Usage notes
- The
json_setfunction follows MySQL-compatible behavior. - It operates as an Upsert (Update or Insert):
- INSERT: If the path does not exist, the value is added to the document.
- UPDATE: If the path already exists, the old value is replaced with the new value.
- If you specifically want to insert only (without updating existing values), use
json_insert. - If you specifically want to update only (without inserting new values), use
json_replace. - Null Handling: To insert a JSON null value, use parse_json(‘null’). Passing a raw SQL NULL as an argument will cause the function to return NULL.
- Note: Wildcard characters (e.g.,
*or**) and array slices (e.g.,[1:3]) are not currently supported injson_pathfor modification. If a path contains these, the update for that path will be ignored to ensure safety.