MySQL JSON: JSON_EXTRACT, ->>, JSON_TABLE and Updating Documents

MySQL 65 min min read Updated: Nov 27, 2026 Intermediate
MySQL JSON: JSON_EXTRACT, ->>, JSON_TABLE and Updating Documents
Intermediate Topic 1 of 3

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

sql CREATE TABLE gadgets (id INT PRIMARY KEY, name VARCHAR(30), specs JSON); INSERT INTO gadgets VALUES (1, 'Phone X', '{"brand": "Acme", "ram_gb": 8, "colors": ["black", "blue"], "dims": {"w": 7, "h": 15}}'), (2, 'Laptop Z', '{"brand": "Zeta", "ram_gb": 16, "colors": ["silver"], "ports": ["usb-c", "hdmi"]}'), (3, 'Watch W', '{"brand": "Acme", "ram_gb": 1}'); SELECT id, name, specs FROM gadgets ORDER BY id;

Result:

idnamespecs
1Phone X{"dims": {"h": 15, "w": 7}, "brand": "Acme", "colors": ["black", "blue"], "ram_gb": 8}
2Laptop Z{"brand": "Zeta", "ports": ["usb-c", "hdmi"], "colors": ["silver"], "ram_gb": 16}
3Watch W{"brand": "Acme", "ram_gb": 1}
sql CREATE TABLE gadgets (id INT PRIMARY KEY, specs JSON); INSERT INTO gadgets VALUES (9, '{"brand": "Acme",}');
Error

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 $:

sql CREATE TABLE gadgets (id INT PRIMARY KEY, name VARCHAR(30), specs JSON); INSERT INTO gadgets VALUES (1, 'Phone X', '{"brand": "Acme", "ram_gb": 8, "colors": ["black", "blue"], "dims": {"w": 7, "h": 15}}'), (2, 'Laptop Z', '{"brand": "Zeta", "ram_gb": 16, "colors": ["silver"]}'); SELECT name, specs->'$.brand' AS json_value, specs->>'$.brand' AS text_value, specs->>'$.dims.w' AS width, specs->>'$.colors[0]' AS first_color FROM gadgets ORDER BY id;

Result:

namejson_valuetext_valuewidthfirst_color
Phone X"Acme"Acme7black
Laptop Z"Zeta"ZetaNULLsilver

A missing path returns NULL, not an error. Laptop Z has no dims, so its width is NULL.

Filtering

sql CREATE TABLE gadgets (id INT PRIMARY KEY, name VARCHAR(30), specs JSON); INSERT INTO gadgets VALUES (1, 'Phone X', '{"brand": "Acme", "ram_gb": 8}'), (2, 'Laptop Z', '{"brand": "Zeta", "ram_gb": 16}'), (3, 'Watch W', '{"brand": "Acme", "ram_gb": 1}'); SELECT name FROM gadgets WHERE specs->>'$.brand' = 'Acme' AND CAST(specs->>'$.ram_gb' AS UNSIGNED) >= 4 ORDER BY id;

Result:

name
Phone X
Cast numbers ->> returns text. Compare numbers after CAST(... AS UNSIGNED), or '10' will sort before '9'.

Searching Arrays and Keys

sql CREATE TABLE gadgets (id INT PRIMARY KEY, name VARCHAR(30), specs JSON); INSERT INTO gadgets VALUES (1, 'Phone X', '{"colors": ["black", "blue"]}'), (2, 'Laptop Z', '{"colors": ["silver"], "ports": ["usb-c"]}'); SELECT name, JSON_CONTAINS(specs->'$.colors', '"blue"') AS has_blue, JSON_CONTAINS_PATH(specs, 'one', '$.ports') AS has_ports, JSON_LENGTH(specs->'$.colors') AS color_count, JSON_KEYS(specs) AS top_keys FROM gadgets ORDER BY id;

Result:

namehas_bluehas_portscolor_counttop_keys
Phone X102["colors"]
Laptop Z011["ports", "colors"]

Note the extra double quotes around blue: the search value must itself be valid JSON.

Changing Documents

FunctionMeaning
JSON_SETAdd or replace a value
JSON_INSERTAdd only if missing
JSON_REPLACEReplace only if present
JSON_REMOVEDelete a key or array element
JSON_ARRAY_APPENDAdd to an array
JSON_MERGE_PATCHMerge documents, null removes keys
sql CREATE TABLE gadgets (id INT PRIMARY KEY, specs JSON); INSERT INTO gadgets VALUES (1, '{"brand": "Acme", "ram_gb": 8, "colors": ["black"]}'); UPDATE gadgets SET specs = JSON_SET(specs, '$.ram_gb', 12, '$.warranty', '2 years'); UPDATE gadgets SET specs = JSON_ARRAY_APPEND(specs, '$.colors', 'blue'); UPDATE gadgets SET specs = JSON_REMOVE(specs, '$.brand'); SELECT specs FROM gadgets;

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

sql SELECT JSON_OBJECT('id', id, 'name', name, 'price', price) AS product_json FROM products WHERE id <= 2 ORDER BY id;

Result:

product_json
{"id": 1, "name": "Laptop", "price": 899.00}
{"id": 2, "name": "Phone", "price": 599.00}
sql SELECT category, JSON_ARRAYAGG(name) AS names FROM (SELECT category, name FROM products ORDER BY name) p GROUP BY category ORDER BY category;

Result:

categorynames
Electronics["Laptop", "Phone", "Headphones"]
Furniture["Desk", "Chair", "Lamp"]
Stationery["Notebook", "Pen Pack"]
sql SELECT JSON_OBJECTAGG(name, price) AS price_map FROM (SELECT name, price FROM products WHERE category = 'Stationery' ORDER BY name) s;

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:

sql SELECT jt.item, jt.qty FROM JSON_TABLE( '[{"item": "pen", "qty": 3}, {"item": "book", "qty": 1}, {"item": "bag", "qty": 2}]', '$[*]' COLUMNS (item VARCHAR(20) PATH '$.item', qty INT PATH '$.qty') ) AS jt ORDER BY jt.qty DESC;

Result:

itemqty
pen3
bag2
book1
sql CREATE TABLE gadgets (id INT PRIMARY KEY, name VARCHAR(30), specs JSON); INSERT INTO gadgets VALUES (1, 'Phone X', '{"colors": ["black", "blue"]}'), (2, 'Laptop Z', '{"colors": ["silver"]}'); SELECT g.name, c.color FROM gadgets g, JSON_TABLE(g.specs, '$.colors[*]' COLUMNS (color VARCHAR(20) PATH '$')) AS c ORDER BY g.id, c.color;

Result:

namecolor
Phone Xblack
Phone Xblue
Laptop Zsilver

Common Beginner Mistakes

MistakeWhat happensFix
Putting everything in JSONHard to index, constrain and joinUse columns for core fields
Comparing ->> numbers as textWrong orderCast to a number
Single quotes inside JSON textInvalid JSONJSON needs double quotes
Expecting -> to give plain textExtra quotes in outputUse ->>
Searching large JSON without an indexFull scansGenerated column or multi-valued index (next lesson)

Practice

  1. Store a settings JSON per user and read the theme key.
  2. Add a new key without touching the others.
  3. Turn an array of tags into rows with JSON_TABLE.
  4. 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.

Quick Summary: JSON type, ->, ->>, JSON_SET, JSON_CONTAINS, JSON_TABLE, JSON_ARRAYAGG.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators