← Back to MySQL Course | Chapter 19: Advanced & Reference | Lesson 2 of 5

MySQL JSON Functions

JSON functions are like tools for reaching inside a little labeled bundle in one cell to read or change just one item without opening the whole thing.
Syntax
sql
JSON_EXTRACT(json_column, '$.key')
json_column->'$.key'
JSON_OBJECT('key1', value1, 'key2', value2)

Working with JSON Columns

MySQL can store an entire JSON document inside a single column, which is a good fit for optional or loosely structured data — like a product's variable attributes — that doesn't map cleanly onto fixed relational columns.

Example: Working with JSON Columns

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red", "size": "M"}');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Extracting JSON Values

JSON_EXTRACT, or the more concise inline arrow operator, lets you pull a specific value out of a JSON column directly inside a query, so you can filter or select on nested JSON data without loading the whole document into your application.

Example: Extracting JSON Values

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red", "size": "M"}');
SELECT id, JSON_EXTRACT(attributes, '$.color') AS color FROM products;
SELECT id, attributes->'$.color' AS color FROM products;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Creating JSON Data

Rather than hand-writing raw JSON text, MySQL provides functions that assemble relational values — numbers, strings, other columns — directly into properly formatted JSON output for you.

Example: Creating JSON Data

sql
SELECT JSON_OBJECT('color', 'red', 'size', 'M') AS built_json;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Modifying JSON Data

JSON_SET updates a value inside an existing JSON document in place, either overwriting an existing key or adding a new one, without you needing to replace the entire document.

Example: Modifying JSON Data

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red"}');
UPDATE products SET attributes = JSON_SET(attributes, '$.size', 'M') WHERE id = 1;
SELECT * FROM products;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Querying by JSON Key Values

Filtering rows based on a value buried inside a JSON column works about as efficiently as filtering on a normal column, especially once a generated column or index is set up over the JSON path you query most often.

Example: Querying by JSON Key Values

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red"}'), (2, '{"color": "blue"}');
SELECT * FROM products WHERE JSON_EXTRACT(attributes, '$.color') = 'red';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Inserting invalid JSON text, which the JSON column rejects.
  2. Using JSON_EXTRACT and expecting unquoted text, when it returns a JSON value with quotes (use ->>).
  3. Writing $.color without the $, or with the wrong case, since JSON keys are case-sensitive.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.