Introduction
The JSON data type lets you store and work with data in a flexible, easy-to-read format inside your database.
CREATE TABLE table_name ( id INT PRIMARY KEY, data JSON );
CREATE TABLE products ( id INT PRIMARY KEY, info JSON );
INSERT INTO products (id, info) VALUES (1, '{"name": "Pen", "price": 1.5}');
SELECT info->>'$.name' AS product_name FROM products WHERE id = 1;
CREATE TABLE users ( id INT PRIMARY KEY, profile JSON ); INSERT INTO users (id, profile) VALUES (1, '{"name": "Alice", "age": 30, "hobbies": ["reading", "hiking"]}'), (2, '{"name": "Bob", "age": 25, "hobbies": ["gaming", "cooking"]}'); SELECT id, profile->>'$.name' AS name, profile->>'$.age' AS age FROM users;
JSON data type in MySQL?JSON without extra modifiers.data JSON. CREATE TABLE t1 (data VARCHAR(255)); uses VARCHAR, not JSON. CREATE TABLE t1 (data TEXT JSON); mixes TEXT and JSON incorrectly. CREATE TABLE t1 (data JSONB); uses JSONB which is PostgreSQL-specific, not MySQL.users with a JSON column info storing {"age": 30, "city": "NY"}, what will this query return?SELECT JSON_EXTRACT(info, '$.city') FROM users WHERE id = 1;
SELECT info->'$.age' FROM users;Assuming
info is a JSON column.info column contains an age greater than 25. Which query correctly does this?Pass the test and this pattern moves unknown