Skip to content

Commit b1d511d

Browse files
committed
JSON_OBJECT_INSERT
1 parent 57fcf1a commit b1d511d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/en/sql-reference/20-sql-functions/10-semi-structured-functions/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This section provides reference information for the semi-structured data functio
3434
- [JSON_STRIP_NULLS](json-strip-nulls.md)
3535
- [JSON_OBJECT](json-object.md)
3636
- [JSON_OBJECT_KEEP_NULL](json-object-keep-null.md)
37+
- [JSON_OBJECT_INSERT](json-object-insert.md)
3738
- [JSON_ARRAY_DISTINCT](json-array-distinct.md)
3839
- [JSON_ARRAY_EXCEPT](json-array-except.md)
3940
- [JSON_ARRAY_INSERT](json-array-insert.md)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)