SQL / MySQL
Advanced
Queries
· 6 min read
MySQL JSON Extract
Lesson 30 of 33 in the SQL / MySQL learning path.
How do I read a field inside a JSON column?
MySQL JSON columns can be queried with -> and ->>. ->> returns an unquoted scalar.
A one-off import blob can be inspected. A field you filter on every page should be a real column.
Syntax & Parameters
SELECT payload->>'$.title' AS title FROM import_rows
Example
SELECT id,
payload->>'$.title' AS title,
payload->>'$.track' AS track
FROM import_rows
WHERE JSON_VALID(payload)
AND payload->>'$.track' = 'php'
ORDER BY id;
-- ->> unquotes the JSON string.
-- A missing path yields NULL.
-- This scan is fine for a staging table.
-- Promote track to a real column before it is a hot filter.
-- JSON_VALID rejects a truncated payload.
Expected Output
Staging rows whose JSON track is php, with title and track extracted as text.
Line-by-Line Walkthrough
- payload is a JSON column on a staging table.
- ->> $.title reads the title text.
- ->> $.track reads the track text.
- JSON_VALID skips broken documents.
- The WHERE compares the extracted track to php.
- A missing key is NULL and fails the comparison.
- The table is staging, not the live lessons table.
- A frequent filter belongs in its own indexed column.
Common Pitfalls & Security
Check the edge before you ship it.
- Filtering on JSON paths usually cannot use a normal index.
- ->> and -> differ. -> keeps JSON quotes around strings.
- A missing path is NULL, not an error.
- Storing the whole catalog as one JSON document makes every query a parse.
Best Practices & Modern Standards
Use JSON for a staging payload or a sparse bag of optional fields.
Copy hot fields into real columns and index those.
Real-World Use
An import table keeps the partner payload. A review query lists php rows before they are copied into lessons.
Frequently Asked Questions
What does ->> do?
It extracts a value and unquotes JSON strings so you can compare them as text.
What if the key is missing?
The result is NULL.
Should lessons be one JSON column?
No. Title, track, and seats should be columns you can index.
Does JSON_VALID matter?
Yes. A truncated payload should be excluded before you extract fields.