Skip to content

Commit 000f59f

Browse files
committed
article about json handling
1 parent 3501947 commit 000f59f

File tree

4 files changed

+557
-4
lines changed

4 files changed

+557
-4
lines changed

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ When loading the page, the value for `:username` will be `NULL` if no value has
311311
']')),
312312
('form', 'This example illustrates the use of the `select` type.
313313
In this select input, the various options are hardcoded, but they could also be loaded from a database table,
314-
using a function to convert the rows into a json array like
314+
[using a function to convert the rows into a json array](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) like
315315
- `json_group_array()` in SQLite,
316316
- `json_agg()` in Postgres,
317317
- `JSON_ARRAYAGG()` in MySQL, or
@@ -341,6 +341,8 @@ The target page will then receive the value as a JSON array of strings, which yo
341341
- the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server.
342342
- in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/lovasoa/SQLpage/tree/main/examples/mysql%20json%20handling)
343343
344+
[More information on how to handle JSON in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
345+
344346
The target page could then look like this:
345347
346348
```sql
@@ -749,7 +751,7 @@ you can use the `dynamic` component to generate the table columns dynamically.
749751
For that, you will need to return JSON objects from your SQL query, where the keys are the column names,
750752
and the values are the cell contents.
751753
752-
Databases offer utilities to generate JSON objects from query results:
754+
Databases [offer utilities to generate JSON objects from query results](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide)
753755
- In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING)
754756
function for a fixed number of columns, or [`json_object_agg`](https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE) for a dynamic number of columns.
755757
- In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns,
@@ -888,6 +890,26 @@ SELECT ''dynamic'' AS component, ''
888890
889891
[View the result of this query, as well as an example of how to generate a dynamic menu
890892
based on the database contents](./examples/dynamic_shell.sql).
893+
', NULL),
894+
('dynamic', '
895+
## Dynamic tables
896+
897+
The `dynamic` component can be used to generate [tables](?component=table#component) with dynamic columns,
898+
using [your database''s JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
899+
900+
For instance, let''s say we have a table with three columns: user_id, name, and role.
901+
We want to create a table where each row is a user, and each column is a role.
902+
We will return a set of json objects that look like this: `{"name": "Alice", "admin": true, "editor": false, "viewer": true}`
903+
```sql
904+
SELECT ''table'' AS component;
905+
SELECT ''dynamic'' AS component,
906+
json_patch(
907+
json_object(''name'', name),
908+
json_object_agg(role, is_admin)
909+
) AS properties
910+
FROM users
911+
GROUP BY name;
912+
```
891913
', NULL);
892914

893915
INSERT INTO component(name, icon, description) VALUES

examples/official-site/sqlpage/migrations/11_json.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ VALUES (
3131
'
3232
Creates an API endpoint that will allow developers to easily query a list of users stored in your database.
3333
34-
You should use the json functions provided by your database to form the value you pass to the `contents` property.
34+
You should use [the json functions provided by your database](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) to form the value you pass to the `contents` property.
3535
To build a json array out of rows from the database, you can use:
3636
- `json_group_array()` in SQLite,
3737
- `json_agg()` in Postgres, or

examples/official-site/sqlpage/migrations/40_fetch.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ select $user_search as title,
3434
In this example, we use the complex form of the function to make an
3535
authenticated POST request, with custom request headers and a custom request body.
3636
37-
We use SQLite''s json functions to build the request body.
37+
We use SQLite''s json functions to build the request body.
38+
See [the list of SQL databases and their JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) for
39+
more information on how to build JSON objects in your database.
3840
3941
```sql
4042
set request = json_object(

0 commit comments

Comments
 (0)