MySQL’s JSON data type lets you store, query, and manipulate structured JSON documents directly inside tables.
JSON columns validate input, preserve key order, and let MySQL index individual paths. TEXT lacks validation and can’t be efficiently queried with JSON functions.
Add a JSON column type when creating or altering a table. Example: ALTER TABLE Products ADD COLUMN specs JSON;
Use JSON_EXTRACT(col, '$.path')
or the shorthand col->'$.path'
. They return the JSON value at the specified path.
JSON_OBJECT()
and JSON_ARRAYAGG()
turn relational rows into well-formed JSON objects or arrays for APIs.
Use JSON_SET()
, JSON_REPLACE()
, and JSON_REMOVE()
to update documents atomically without rewriting the full column.
Create a virtual generated column that extracts the path, then index it: ALTER TABLE Orders ADD order_year INT GENERATED ALWAYS AS (JSON_EXTRACT(details,'$.year')) STORED, ADD INDEX(order_year);
Yes—use a generated column or JSON_EXTRACT in the join condition, but be aware that functions in joins may prevent index use.
Validate JSON on insert, keep documents small, index frequently used paths, and back up with traditional relational columns when values must be enforced.
Yes. Arrays are fully supported; use '$[index]'
in JSON_EXTRACT to reach elements.
No. JSON adds flexibility but can be slower for analytical queries. Keep structured data relational when possible.
The maximum size is 4GB, but performance degrades well before that. Aim for documents under a few kilobytes.