|
| 1 | +--- |
| 2 | +title: JSON_OBJECT_INSERT |
| 3 | +--- |
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
| 5 | + |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.647"/> |
| 7 | + |
| 8 | +Inserts or updates a key-value pair in a JSON object. |
| 9 | + |
| 10 | +## Syntax |
| 11 | + |
| 12 | +```sql |
| 13 | +JSON_OBJECT_INSERT(<json_object>, <key>, <value>[, true | false ]) |
| 14 | +``` |
| 15 | + |
| 16 | +| Parameter | Description | | |
| 17 | +|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---| |
| 18 | +| `<json_object>` | The input JSON object. | | |
| 19 | +| `<key>` | The key to be inserted or updated. | | |
| 20 | +| `<value>` | The value to assign to the key. | | |
| 21 | +| `[, true | false]` | A boolean flag that controls whether to replace the value if the specified key already exists in the JSON object. If `true`, the function replaces the value if the key already exists. If `false` (or omitted), an error occurs if the key exists. | | |
| 22 | + |
| 23 | +## Return Type |
| 24 | + |
| 25 | +Returns the updated JSON object. |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +This example demonstrates how to insert a new key 'c' with the value 3 into the existing JSON object: |
| 30 | + |
| 31 | +```sql |
| 32 | +SELECT JSON_OBJECT_INSERT('{"a":1,"b":2,"d":4}'::variant, 'c', 3); |
| 33 | + |
| 34 | +┌────────────────────────────────────────────────────────────┐ |
| 35 | +│ json_object_insert('{"a":1,"b":2,"d":4}'::VARIANT, 'c', 3) │ |
| 36 | +├────────────────────────────────────────────────────────────┤ |
| 37 | +│ {"a":1,"b":2,"c":3,"d":4} │ |
| 38 | +└────────────────────────────────────────────────────────────┘ |
| 39 | +``` |
| 40 | + |
| 41 | +This example shows how to update the value of an existing key 'a' from 1 to 10 using the boolean flag set to `true`, allowing the key's value to be replaced: |
| 42 | + |
| 43 | +```sql |
| 44 | +SELECT JSON_OBJECT_INSERT('{"a":1,"b":2,"d":4}'::variant, 'a', 10, true); |
| 45 | + |
| 46 | +┌───────────────────────────────────────────────────────────────────┐ |
| 47 | +│ json_object_insert('{"a":1,"b":2,"d":4}'::VARIANT, 'a', 10, TRUE) │ |
| 48 | +├───────────────────────────────────────────────────────────────────┤ |
| 49 | +│ {"a":10,"b":2,"d":4} │ |
| 50 | +└───────────────────────────────────────────────────────────────────┘ |
| 51 | +``` |
| 52 | + |
| 53 | +This example demonstrates an error that occurs when trying to insert a value for an existing key 'a' without specifying the boolean flag set to `true`: |
| 54 | + |
| 55 | +```sql |
| 56 | +SELECT JSON_OBJECT_INSERT('{"a":1,"b":2,"d":4}'::variant, 'a', 10); |
| 57 | + |
| 58 | +error: APIError: ResponseError with 1006: ObjectDuplicateKey while evaluating function `json_object_insert('{"a":1,"b":2,"d":4}', 'a', 10)` in expr `json_object_insert('{"a":1,"b":2,"d":4}', 'a', 10)` |
| 59 | +``` |
0 commit comments