Skip to content

Commit 0db588f

Browse files
committed
json in sql
1 parent fb43436 commit 0db588f

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

examples/official-site/sqlpage/migrations/50_blog_json.sql

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
INSERT INTO blog_posts (title, description, icon, created_at, content)
32
VALUES
43
(
@@ -62,9 +61,9 @@ FROM users;
6261
6362
| user_json |
6463
|-----------|
65-
| {"name":"Alice","birthday":"1990-01-15"} |
66-
| {"name":"Bob","birthday":"1985-05-22"} |
67-
| {"name":"Charlie","birthday":"1992-09-30"} |
64+
| `{"name":"Alice","birthday":"1990-01-15"}` |
65+
| `{"name":"Bob","birthday":"1985-05-22"}` |
66+
| `{"name":"Charlie","birthday":"1992-09-30"}` |
6867
6968
### Creating a JSON array
7069
@@ -75,9 +74,9 @@ FROM users;
7574
7675
| user_array |
7776
|------------|
78-
| ["Alice","1990-01-15","Admin"] |
79-
| ["Bob","1985-05-22","User"] |
80-
| ["Charlie","1992-09-30","User"] |
77+
| `["Alice","1990-01-15","Admin"]` |
78+
| `["Bob","1985-05-22","User"]` |
79+
| `["Charlie","1992-09-30","User"]` |
8180
8281
### Aggregating multiple values into a JSON array
8382
@@ -88,18 +87,18 @@ FROM users;
8887
8988
| names |
9089
|-------|
91-
| ["Alice","Bob","Charlie"] |
90+
| `["Alice","Bob","Charlie"]` |
9291
9392
### Aggregating values into a JSON object
9493
9594
```sql
96-
SELECT json_group_object(name, birthday) AS name_birthday_map
95+
SELECT json_group_object(name, group_name) AS name_group_map
9796
FROM users;
9897
```
9998
100-
| name_birthday_map |
99+
| name_group_map |
101100
|-------------------|
102-
| {"Alice":"1990-01-15","Bob":"1985-05-22","Charlie":"1992-09-30"} |
101+
| `{"Alice":"Admin", "Bob":"User", "Charlie":"User"}` |
103102
104103
105104
### Iterating over a JSON array
@@ -124,18 +123,27 @@ The `json_each()` function returns a table with several columns. The most common
124123
125124
For more complex JSON structures, you can use the `json_tree()` function, which recursively walks through the entire JSON structure.
126125
127-
These iteration functions can be used to test whether a value is present in a JSON array. For instance, to create a
128-
[multi-value select dropdown](documentation.sql?component=form#component) with pre-selected values, you can use the following query:
126+
These iteration functions can be used to check if specific values exist in a JSON array.
127+
Here''s a practical example:
128+
Let''s say you have a form with a [multiple-choice dropdown](documentation.sql?component=form#component) that allows selecting multiple users.
129+
Some users might already be selected, and their IDs are stored in a JSON array passed as an URL parameter called `$selected_ids`.
130+
You can create this dropdown using the following query:
129131
130132
```sql
131133
select json_group_array(json_object(
132-
''label'', name
134+
''label'', name,
133135
''value'', id,
134136
''selected'', id in (select value from json_each_text($selected_ids))
135137
)) as options
136138
from users;
137139
```
138140
141+
This query will:
142+
1. Create a dropdown option for each user
143+
2. Use their name as the display label
144+
3. Use their ID as the value
145+
4. Mark the option as selected if the user's ID exists in the $selected_ids array
146+
139147
### Combining two JSON objects
140148

141149
SQLite provides the `json_patch()` function to combine two JSON objects. This function takes two JSON objects as arguments and returns a new JSON object that is the result of merging the two input objects.
@@ -156,26 +164,35 @@ See [the list of JSON functions in PostgreSQL](https://www.postgresql.org/docs/c
156164
### Creating a JSON object
157165

158166
```sql
159-
SELECT jsonb_build_object(''name'', name, ''birthday'', birthday) AS user_json FROM users;
167+
SELECT
168+
jsonb_build_object(
169+
''name'', name,
170+
''birthday'', birthday
171+
) AS user_json
172+
FROM users;
160173
```
161174

162175
| user_json |
163176
|-----------|
164-
| {"name": "Alice", "birthday": "1990-01-15"} |
165-
| {"name": "Bob", "birthday": "1985-05-22"} |
166-
| {"name": "Charlie", "birthday": "1992-09-30"} |
177+
| `{"name":"Alice","birthday":"1990-01-15"}` |
178+
| `{"name":"Bob","birthday":"1985-05-22"}` |
179+
| `{"name":"Charlie","birthday":"1992-09-30"}` |
167180

168181
### Creating a JSON array
169182

170183
```sql
171-
SELECT jsonb_build_array(name, birthday, group_name) AS user_array FROM users;
184+
SELECT
185+
jsonb_build_array(
186+
name, birthday, group_name
187+
) AS user_array
188+
FROM users;
172189
```
173190

174191
| user_array |
175192
|------------|
176-
| ["Alice", "1990-01-15", "Admin"] |
177-
| ["Bob", "1985-05-22", "User"] |
178-
| ["Charlie", "1992-09-30", "User"] |
193+
| `["Alice", "1990-01-15", "Admin"]` |
194+
| `["Bob", "1985-05-22", "User"]` |
195+
| `["Charlie", "1992-09-30", "User"]` |
179196

180197
### Aggregating multiple values into a JSON array
181198

@@ -185,18 +202,21 @@ SELECT jsonb_agg(name) AS names FROM users;
185202

186203
| names |
187204
|-------|
188-
| ["Alice", "Bob", "Charlie"] |
205+
| `["Alice","Bob","Charlie"]` |
189206

190207
### Aggregating values into a JSON object
191208

192209
```sql
193-
SELECT jsonb_object_agg(name, birthday) AS name_birthday_map
210+
SELECT
211+
jsonb_object_agg(
212+
name, birthday
213+
) AS name_birthday_map
194214
FROM users;
195215
```
196216

197217
| name_birthday_map |
198218
|-------------------|
199-
| `{"Alice": "1990-01-15", "Bob": "1985-05-22", "Charlie": "1992-09-30"}` |
219+
| `{"Alice":"1990-01-15","Bob":"1985-05-22","Charlie":"1992-09-30"}` |
200220

201221

202222
### Iterating over a JSON array
@@ -278,9 +298,9 @@ FROM users;
278298

279299
| user_json |
280300
|-----------|
281-
| {"name": "Alice", "birthday": "1990-01-15"} |
282-
| {"name": "Bob", "birthday": "1985-05-22"} |
283-
| {"name": "Charlie", "birthday": "1992-09-30"} |
301+
| `{"name":"Alice","birthday":"1990-01-15"}` |
302+
| `{"name":"Bob","birthday":"1985-05-22"}` |
303+
| `{"name":"Charlie","birthday":"1992-09-30"}` |
284304

285305
### Creating a JSON array
286306

@@ -291,9 +311,9 @@ FROM users;
291311

292312
| user_array |
293313
|------------|
294-
| ["Alice", "1990-01-15", "Admin"] |
295-
| ["Bob", "1985-05-22", "User"] |
296-
| ["Charlie", "1992-09-30", "User"] |
314+
| `["Alice","1990-01-15","Admin"]` |
315+
| `["Bob","1985-05-22","User"]` |
316+
| `["Charlie","1992-09-30","User"]` |
297317

298318
### Aggregating multiple values into a JSON array
299319

@@ -304,7 +324,7 @@ FROM users;
304324

305325
| names |
306326
|-------|
307-
| ["Alice", "Bob", "Charlie"] |
327+
| `["Alice","Bob","Charlie"]` |
308328

309329
### Aggregating values into a JSON object
310330

@@ -315,7 +335,7 @@ FROM users;
315335

316336
| name_birthday_map |
317337
|-------------------|
318-
| {"Alice": "1990-01-15", "Bob": "1985-05-22", "Charlie": "1992-09-30"} |
338+
| `{"Alice":"1990-01-15","Bob":"1985-05-22","Charlie":"1992-09-30"}` |
319339

320340
### Iterating over a JSON array
321341

@@ -442,9 +462,9 @@ FROM users;
442462

443463
| user_json |
444464
|-----------|
445-
| {"name":"Alice","birthday":"1990-01-15"} |
446-
| {"name":"Bob","birthday":"1985-05-22"} |
447-
| {"name":"Charlie","birthday":"1992-09-30"} |
465+
| `{"name":"Alice","birthday":"1990-01-15"}` |
466+
| `{"name":"Bob","birthday":"1985-05-22"}` |
467+
| `{"name":"Charlie","birthday":"1992-09-30"}` |
448468

449469
Alternatively, you can use the `JSON_OBJECT` function:
450470

@@ -464,9 +484,9 @@ FROM users;
464484

465485
| user_array |
466486
|------------|
467-
| [{"name":"Alice","birthday":"1990-01-15","group_name":"Admin"}] |
468-
| [{"name":"Bob","birthday":"1985-05-22","group_name":"User"}] |
469-
| [{"name":"Charlie","birthday":"1992-09-30","group_name":"User"}] |
487+
| `[{"name":"Alice","birthday":"1990-01-15","group_name":"Admin"}]` |
488+
| `[{"name":"Bob","birthday":"1985-05-22","group_name":"User"}]` |
489+
| `[{"name":"Charlie","birthday":"1992-09-30","group_name":"User"}]` |
470490

471491
You can also use the `JSON_ARRAY` function:
472492

@@ -485,7 +505,7 @@ SELECT (SELECT name FROM users FOR JSON PATH) AS names;
485505

486506
| names |
487507
|-------|
488-
| [{"name":"Alice"},{"name":"Bob"},{"name":"Charlie"}] |
508+
| `[{"name":"Alice"},{"name":"Bob"},{"name":"Charlie"}]` |
489509

490510
Alternatively, use the `JSON_ARRAYAGG` function:
491511

0 commit comments

Comments
 (0)