MySQL supports JSON data type, allowing structured data storage inside columns. JSON indexing in MySQL means extracting values from JSON objects or arrays using path expressions and built-in JSON functions.
$JSON_EXTRACT() and -> operator are usedJSON_EXTRACT(json_col,'$.key') → Access object valuejson_col->'$.key' → Shorthand extractionjson_col->>'$.key' → Extract as plain text$.array[index] → Access array element
// Creating table with JSON column
CREATE TABLE students (
id INT PRIMARY KEY,
data JSON
);
// Inserting JSON data
INSERT INTO students VALUES
(1,'{"name":"Rahul","age":22,"skills":["HTML","CSS","SQL"]}');
// Extracting JSON object value
SELECT data->>'$.name' AS student_name
FROM students;
// Extracting array element using index
SELECT data->>'$.skills[1]' AS second_skill
FROM students;
// Accessing nested JSON using JSON_EXTRACT
SELECT JSON_EXTRACT(data,'$.age') AS student_age
FROM students;
Rahul
CSS
22
The JSON path expression navigates through keys and array indexes to fetch the required values from the JSON column.
Test your JSON path skills live!
JSON_VALID()->> to avoid quoted output