SQL - JSON_EXTRACT() in SQL
JSON_EXTRACT() is a built-in SQL function used to retrieve specific values from a JSON column in a table.
Syntax
JSON_EXTRACT(json_column, '$.key')
- json_column – the column containing JSON data
- $.key – path to the key you want to extract
- $ refers to the root of JSON
- Use . for nested keys
- Use [*] for arrays
Example Table
CREATE TABLE users (
id INT,
profile JSON
);
INSERT INTO users VALUES
(1, '{"name": "Arun Kumar", "age": 28, "skills": ["SQL", "CSS"]}');
Extract Single Value
SELECT JSON_EXTRACT(profile, '$.name') AS name
FROM users;
Result:
name
-----
"Arun Kumar"
Extract Nested Value
SELECT JSON_EXTRACT(profile, '$.skills[0]') AS first_skill
FROM users;
Result:
first_skill
------------
"SQL"
Key Points
- Returns value in JSON format (quotes included)
- Works with nested objects and arrays
- Can be combined with WHERE to filter data