Skip to content

Commit b8f6e75

Browse files
authored
Merge pull request #336 from percona/ps-9256
PS-9256 Add JSON information
2 parents 34f5e01 + eaafca2 commit b8f6e75

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

docs/json-overview.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# # JSON in MySQL
2+
3+
JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format that is easy for humans to read and write. It is also easy for machines to parse and generate. MySQL supports JSON data type, allowing you to store JSON documents in your database.
4+
5+
## Create a table with JSON Data Type
6+
7+
Create a table that includes a column with the JSON data type.
8+
9+
```{.bash data-prompt="mysql>"}
10+
mysql> CREATE TABLE users (
11+
id INT AUTO_INCREMENT PRIMARY KEY,
12+
name VARCHAR(255) NOT NULL,
13+
info JSON
14+
);
15+
```
16+
17+
The columns are the following:
18+
19+
* `id` is an auto-incremented primary key.
20+
21+
* `name` is a column for storing the user's name.
22+
23+
* `info` is a column for storing JSON data.
24+
25+
## Insert JSON Data
26+
27+
Insert the JSON data into the table using the `INSERT` statement. The `name` column stores the user's name. The `info` column stores JSON data using the `JSON_OBJECT` function. This function creates a JSON object with key-value pairs.
28+
29+
```{.bash data-prompt="mysql>"}
30+
mysql> INSERT INTO users (name, info) VALUES (
31+
'John Doe',
32+
JSON_OBJECT('age', 30, 'city', 'New York', 'email', '[email protected]')
33+
);
34+
```
35+
36+
## Query JSON Data
37+
38+
You can query JSON data using the `SELECT` statement. The `name` column retrieves the user's name. The `info->>'$.age'` expression retrieves the value of the `age` key from the JSON object stored in the `info` column.
39+
40+
```{.bash data-prompt="mysql>"}
41+
mysql> SELECT name, info->>'$.age' AS age FROM users;
42+
```
43+
44+
## Update JSON Data
45+
46+
You can update JSON data using the `UPDATE` statement. The `JSON_SET` function updates the value of the `age` key in the JSON object stored in the `info` column. The `WHERE` clause specifies that only the row with the name 'John Doe' should be updated.
47+
48+
```{.bash data-prompt="mysql>"}
49+
mysql> UPDATE users
50+
SET info = JSON_SET(info, '$.age', 31)
51+
WHERE name = 'John Doe';
52+
```
53+
54+
## Delete JSON Data
55+
56+
You can delete JSON data using the `DELETE` statement. This statement removes rows from the `users` table where the `city` key in the JSON object stored in the `info` column has the value 'New York'.
57+
58+
```{.bash data-prompt="mysql>"}
59+
mysql> DELETE FROM users WHERE info->>'$.city' = 'New York';
60+
```
61+
62+
## Add New Key-Value Pairs to JSON Data
63+
64+
You can add new key-value pairs to existing JSON data using the `JSON_SET` function. The `JSON_SET` function adds a new key `phone` with the value '123-456-7890' to the JSON object stored in the `info` column.
65+
66+
```{.bash data-prompt="mysql>"}
67+
mysql> UPDATE users
68+
SET info = JSON_SET(info, '$.phone', '123-456-7890')
69+
WHERE name = 'John Doe';
70+
```
71+
72+
## Remove Key-Value Pairs from JSON Data
73+
74+
You can remove key-value pairs from existing JSON data using the `JSON_REMOVE` function. This function removes the `email` key from the JSON object stored in the `info` column.
75+
76+
```{.bash data-prompt="mysql>"}
77+
mysql> UPDATE users
78+
SET info = JSON_REMOVE(info, '$.email')
79+
WHERE name = 'John Doe';
80+
```
81+
82+
## Use JSON Functions
83+
84+
MySQL provides several functions to work with JSON data.
85+
86+
### `JSON_EXTRACT`
87+
88+
You can extract data from a JSON document using the `JSON_EXTRACT` function. This function extracts the value of the `city` key from the JSON object stored in the `info` column.
89+
90+
```{.bash data-prompt="mysql>"}
91+
mysql> SELECT JSON_EXTRACT(info, '$.city') AS city FROM users WHERE name = 'John Doe';
92+
```
93+
94+
### `JSON_ARRAY`
95+
96+
You can create a JSON array using the `JSON_ARRAY` function. This function creates a JSON array with the values 'apple', 'banana', and 'cherry'.
97+
98+
```{.bash data-prompt="mysql>"}
99+
mysql> INSERT INTO users (name, info) VALUES (
100+
'Jane Smith',
101+
JSON_ARRAY('apple', 'banana', 'cherry')
102+
);
103+
```
104+
105+
### `JSON_CONTAINS`
106+
107+
You can check if a JSON document contains a specific value using the `JSON_CONTAINS` function. This function checks if the `info` column contains the value 'New York' for the `city` key.
108+
109+
```{.bash data-prompt="mysql>"}
110+
mysql> SELECT name FROM users WHERE JSON_CONTAINS(info, '"New York"', '$.city');
111+
```

mkdocs-base.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ nav:
248248
- stored-procedure-variables.md
249249
- triggers.md
250250
- troubleshooting-sql.md
251+
- JSON:
252+
- json-overview.md
251253

252254
- Manage:
253255
- Database management:

0 commit comments

Comments
 (0)