Storing & Querying Native JSON in MySQL
Storing & Querying Native JSON in MySQL
Historically, storing semi-structured or polymorphic data in relational databases required either awkward Entity-Attribute-Value (EAV) anti-patterns or flat TEXT columns.
Starting in MySQL 5.7 and heavily expanded in MySQL 8.0, MySQL features a Native JSON Data Type. Unlike storing JSON strings in TEXT, native JSON provides:
- 1Automatic Document Validation: Rejects malformed JSON syntax on insert.
- 2Binary Storage Format: Documents are parsed into a fast binary tree format (
B-Jdoc), enabling random-access key lookups without parsing the entire text string!
Creating Tables with JSON Columns
Inserting JSON Documents
You can insert valid JSON string literals directly:
If an application attempts to insert malformed JSON (e.g., missing closing quotes), MySQL immediately rejects the query: ERROR 3140 (22032): Invalid JSON text in argument 1 to function json_valid: "Syntax error".
Querying JSON Using Paths: -> and ->>
MySQL provides two JSON extraction operators:
->(JSON_EXTRACT): Extracts data preserving JSON formatting (strings include quotes).->>(Inline Path Operator): Extracts data unquoted as plain SQL text!
Filtering in WHERE Clauses with JSON
Multiple Choice Questions
1. What is the primary advantage of MySQL's native JSON data type over storing JSON strings in a TEXT column?
A. JSON columns take zero disk space B. Automatic syntax validation and internal binary tree storage allowing fast random-access key retrieval C. JSON columns can only store English words D. Automatically sends data to MongoDB Answer: B Explanation: Native JSON validates syntax and stores documents in an optimized binary format, allowing key extraction without full document deserialization.
2. What is the difference between the -> and ->> operators in MySQL JSON queries?
A. -> is for numbers, ->> is for dates B. -> returns values with JSON formatting/quotes, while ->> returns unquoted plain text strings C. ->> is deprecated D. -> can only query root keys Answer: B Explanation: -> is synonymous with JSON_EXTRACT (retaining quotes), whereas ->> unquotes the resulting string value.
3. How do you reference the first element of an array inside a JSON path expression?
A. $.array(1) B. $.array[0] C. $.array.first D. $.array->1 Answer: B Explanation: JSON path syntax uses standard 0-based bracket indexing (e.g., $.tags[0]).
4. Which MySQL function tests whether a JSON document or array contains a specific candidate value?
A. JSON_MATCH() B. JSON_CONTAINS() C. JSON_HAS() D. JSON_EXISTS() Answer: B Explanation: JSON_CONTAINS(target_json, candidate_json) returns 1 if the target document contains the candidate element.
5. What happens if an INSERT statement provides invalid JSON text to a native JSON column?
A. MySQL converts it to an empty string B. MySQL rejects the statement with Error 3140 (Invalid JSON text) C. MySQL stores the file on disk as binary D. It triggers a server reboot Answer: B Explanation: Native JSON strictly validates syntax upon write; any malformed syntax causes the statement to fail immediately.
Modifying JSON Documents (JSON_SET, JSON_INSERT, JSON_REMOVE)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.