You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -124,18 +123,27 @@ The `json_each()` function returns a table with several columns. The most common
124
123
125
124
For more complex JSON structures, you can use the `json_tree()` function, which recursively walks through the entire JSON structure.
126
125
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:
129
131
130
132
```sql
131
133
select json_group_array(json_object(
132
-
''label'', name
134
+
''label'', name,
133
135
''value'', id,
134
136
''selected'', id in (select value from json_each_text($selected_ids))
135
137
)) as options
136
138
from users;
137
139
```
138
140
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
+
139
147
### Combining two JSON objects
140
148
141
149
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
156
164
### Creating a JSON object
157
165
158
166
```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;
160
173
```
161
174
162
175
| user_json |
163
176
|-----------|
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"}` |
167
180
168
181
### Creating a JSON array
169
182
170
183
```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;
172
189
```
173
190
174
191
| user_array |
175
192
|------------|
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"]` |
179
196
180
197
### Aggregating multiple values into a JSON array
181
198
@@ -185,18 +202,21 @@ SELECT jsonb_agg(name) AS names FROM users;
185
202
186
203
| names |
187
204
|-------|
188
-
| ["Alice","Bob","Charlie"] |
205
+
| `["Alice","Bob","Charlie"]` |
189
206
190
207
### Aggregating values into a JSON object
191
208
192
209
```sql
193
-
SELECT jsonb_object_agg(name, birthday) AS name_birthday_map
0 commit comments