Skip to content

Commit 0dfca98

Browse files
committed
remove reference to unsupported FOR ORDINALITY in json reference article
see #658
1 parent 4018711 commit 0dfca98

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -324,28 +324,24 @@ MySQL provides the JSON_TABLE() function to iterate over JSON arrays. This power
324324
Here''s an example of how to use JSON_TABLE() to iterate over a JSON array:
325325
326326
```sql
327-
SELECT jt.*
327+
SELECT jt.name
328328
FROM JSON_TABLE(
329329
''["Alice", "Bob", "Charlie"]'',
330-
''$[*]'' COLUMNS(
331-
row_num FOR ORDINALITY,
332-
name VARCHAR(50) PATH ''$''
333-
)
330+
''$[*]'' COLUMNS( name VARCHAR(50) PATH ''$'' )
334331
) AS jt;
335332
```
336333
337-
| row_num | name |
338-
|---------|---------|
339-
| 1 | Alice |
340-
| 2 | Bob |
341-
| 3 | Charlie |
334+
| name |
335+
|---------|
336+
| Alice |
337+
| Bob |
338+
| Charlie |
342339
343340
In this example:
344341
- The first argument to JSON_TABLE() is the JSON array.
345342
- `''$[*]''` is the path expression that selects all elements of the array.
346-
- COLUMNS clause defines the structure of the output table:
347-
- `row_num FOR ORDINALITY` creates a column that numbers the rows.
348-
- `name VARCHAR(50) PATH ''$''` creates a column that contains the value of each array element.
343+
- The `COLUMNS` clause defines the structure of the output table. In our case, we want a single column named `name`:
344+
- `name VARCHAR(50) PATH ''$''` creates a text column that contains the raw value of each array element in its entirety (`$` is the current element).
349345
350346
You can also use JSON_TABLE() with more complex JSON structures:
351347
@@ -354,24 +350,23 @@ SELECT jt.*
354350
FROM JSON_TABLE(
355351
''[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}, {"id": 3, "name": "Charlie"}]'',
356352
''$[*]'' COLUMNS(
357-
row_num FOR ORDINALITY,
358-
id INT PATH ''$.id'',
359-
name VARCHAR(50) PATH ''$.name''
353+
id INT PATH ''$.id'',
354+
name VARCHAR(50) PATH ''$.name''
360355
)
361356
) AS jt;
362357
```
363358
364-
| row_num | id | name |
365-
|---------|------|---------|
366-
| 1 | 1 | Alice |
367-
| 2 | 2 | Bob |
368-
| 3 | 3 | Charlie |
359+
| id | name |
360+
|----|---------|
361+
| 1 | Alice |
362+
| 2 | Bob |
363+
| 3 | Charlie |
369364
370365
This approach allows you to easily iterate over JSON arrays and access their elements in a tabular format, which can be very useful for further processing or joining with other tables in your database.
371366
372367
### Iterating over a JSON object
373368
374-
MySQL provides the `JSON_TABLE` function to iterate over JSON objects:
369+
The `JSON_TABLE` function can also be used to iterate over JSON objects:
375370
376371
```sql
377372
SELECT jt.*
@@ -388,6 +383,23 @@ FROM JSON_TABLE(
388383
| "Alice" |
389384
| "1990-01-15" |
390385
386+
#### Iterating over key-value pairs
387+
388+
You can use the `JSON_KEYS()` function to retrieve the list of keys in a JSON object as a JSON array,
389+
then use that array to iterate over the keys of a JSON object:
390+
391+
```sql
392+
SELECT json_key, json_extract(json_str, CONCAT(''$.'', json_key)) as json_value
393+
FROM
394+
(select ''{"name": "Alice", "birthday": "1990-01-15"}'' as json_str) AS my_json,
395+
JSON_TABLE(json_keys(json_str), ''$[*]'' COLUMNS (json_key JSON PATH ''$'')) AS json_keys;
396+
```
397+
398+
| json_key | json_value |
399+
|----------|------------|
400+
| name | Alice |
401+
| birthday | 1990-01-15 |
402+
391403
### Querying JSON data
392404
393405
MySQL allows you to query JSON data using the `->` and `->>` operators:

0 commit comments

Comments
 (0)