← Back to MySQL Course | Chapter 3: Data Types | Lesson 8 of 8

JSON Data Type

A JSON column is like a mini folder tucked into a single cell, holding a little bundle of labeled information, and MySQL makes sure the bundle is written correctly.
Syntax
sql
column_name JSON

SELECT column_name->'$.key' FROM table_name;

JSON Columns

The native JSON column type stores a structured document directly in a single column and automatically validates that whatever you insert is syntactically valid JSON, rejecting malformed input outright.

Example: JSON Columns

sql
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');

Extracting JSON Data

The -> operator (or the more verbose JSON_EXTRACT function) lets you pull a specific field out of a stored JSON document directly in a query, without reading and parsing the whole document in application code.

Example: Extracting JSON Data

sql
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');
SELECT config->'$.theme' FROM settings;

Modifying JSON Documents

JSON_SET and JSON_INSERT let you update or add a single key inside a stored document in place, without needing to fetch, modify, and rewrite the entire JSON blob from your application.

Example: Modifying JSON Documents

sql
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');
UPDATE settings SET config = JSON_SET(config, '$.theme', 'light');

JSON Validation

JSON_VALID() checks whether an arbitrary string is well-formed JSON, and JSON_CONTAINS_PATH() checks whether a specific path exists in a document — both are useful guards before trusting external input.

Example: JSON Validation

sql
SELECT JSON_VALID('{"a": 1}') AS is_valid, JSON_CONTAINS_PATH('{"a": 1}', 'one', '$.a') AS has_path;

JSON Arrays

JSON_ARRAY() and related functions let you build a JSON array directly inside a SELECT statement, which is handy for shaping query results into a format an API endpoint can return as-is.

Example: JSON Arrays

sql
SELECT JSON_ARRAY('red', 'green', 'blue') AS colors;
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 such as {theme: dark} with single quotes inside, which the JSON column rejects.
  2. Using config->'$.theme' and expecting plain text, when it returns a quoted JSON value (use ->> to unquote).
  3. Forgetting the $ at the start of a JSON path, like theme instead of '$.theme'.
Chapter Summary
  • MySQL has numeric, string, and date and time data types for storing different kinds of values.
  • ENUM, SET, and BOOLEAN handle fixed choices and true or false values.
  • BLOB and binary types store raw data, and the JSON type stores structured JSON documents.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.