When to Use JSON
The JSON type stores a validated JSON document. It suits optional or varying attributes (settings, product specs, API payloads). Keep important, frequently filtered fields as normal columns, because normal columns are simpler, smaller and easier to index and constrain.
Create and Insert
Result:
| id | name | specs |
|---|---|---|
| 1 | Phone X | {"dims": {"h": 15, "w": 7}, "brand": "Acme", "colors": ["black", "blue"], "ram_gb": 8} |
| 2 | Laptop Z | {"brand": "Zeta", "ports": ["usb-c", "hdmi"], "colors": ["silver"], "ram_gb": 16} |
| 3 | Watch W | {"brand": "Acme", "ram_gb": 1} |
ERROR 3140: Invalid JSON text: "Missing a name for object member." at position 17 in value for column 'gadgets.specs'.
MySQL rejects invalid JSON on insert, which is a big advantage over a text column.
Reading Values: -> and ->>
col->'$.key' returns JSON (strings keep their quotes). col->>'$.key' returns plain text. The path starts at $:
Result:
| name | json_value | text_value | width | first_color |
|---|---|---|---|---|
| Phone X | "Acme" | Acme | 7 | black |
| Laptop Z | "Zeta" | Zeta | NULL | silver |
A missing path returns NULL, not an error. Laptop Z has no dims, so its width is NULL.
Filtering
Result:
| name |
|---|
| Phone X |
->> returns text. Compare numbers after CAST(... AS UNSIGNED), or '10' will sort before '9'.
Searching Arrays and Keys
Result:
| name | has_blue | has_ports | color_count | top_keys |
|---|---|---|---|---|
| Phone X | 1 | 0 | 2 | ["colors"] |
| Laptop Z | 0 | 1 | 1 | ["ports", "colors"] |
Note the extra double quotes around blue: the search value must itself be valid JSON.
Changing Documents
| Function | Meaning |
|---|---|
JSON_SET | Add or replace a value |
JSON_INSERT | Add only if missing |
JSON_REPLACE | Replace only if present |
JSON_REMOVE | Delete a key or array element |
JSON_ARRAY_APPEND | Add to an array |
JSON_MERGE_PATCH | Merge documents, null removes keys |
Result:
| specs |
|---|
| {"colors": ["black", "blue"], "ram_gb": 12, "warranty": "2 years"} |
MySQL stores JSON in a normalized form, so key order may change and spaces are reduced.
Building JSON from Rows
Result:
| product_json |
|---|
| {"id": 1, "name": "Laptop", "price": 899.00} |
| {"id": 2, "name": "Phone", "price": 599.00} |
Result:
| category | names |
|---|---|
| Electronics | ["Laptop", "Phone", "Headphones"] |
| Furniture | ["Desk", "Chair", "Lamp"] |
| Stationery | ["Notebook", "Pen Pack"] |
Result:
| price_map |
|---|
| {"Notebook": 3.50, "Pen Pack": 5.00} |
JSON_TABLE: JSON into Rows
JSON_TABLE (8.0.4+) turns a JSON array into a relational table you can join and filter:
Result:
| item | qty |
|---|---|
| pen | 3 |
| bag | 2 |
| book | 1 |
Result:
| name | color |
|---|---|
| Phone X | black |
| Phone X | blue |
| Laptop Z | silver |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Putting everything in JSON | Hard to index, constrain and join | Use columns for core fields |
Comparing ->> numbers as text | Wrong order | Cast to a number |
| Single quotes inside JSON text | Invalid JSON | JSON needs double quotes |
Expecting -> to give plain text | Extra quotes in output | Use ->> |
| Searching large JSON without an index | Full scans | Generated column or multi-valued index (next lesson) |
Practice
- Store a settings JSON per user and read the
themekey. - Add a new key without touching the others.
- Turn an array of tags into rows with
JSON_TABLE. - Build one JSON array of product names per category.
Conclusion
Use the JSON type for flexible attributes, read with ->>, change with JSON_SET and friends, build with JSON_OBJECT and JSON_ARRAYAGG, and unnest with JSON_TABLE.

