How to retrieve JSON data from MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
MySQL JSON columns let you store flexible document-style data while still querying with SQL. The important part is choosing the right retrieval method for your use case: display, filtering, or aggregation. Good JSON retrieval uses path extraction functions, proper typing, and indexes for frequently queried keys.
Create a Table with JSON Data
Start with a reproducible sample table.
Now you can query nested values without denormalizing every key into separate columns.
Extract JSON Fields in SELECT Queries
Use JSON_EXTRACT or shorthand operators.
Notes:
->returns JSON value.->>returns unquoted text.JSON_UNQUOTE(JSON_EXTRACT(...))is equivalent to->>.
For numeric operations, cast extracted values explicitly.
Filter Rows by JSON Content
JSON path filters are common in APIs and dashboards.
This is readable, but performance can degrade on large tables without indexes.
Use Generated Columns for Indexing
For hot query paths, extract JSON fields into generated columns and index them.
Now filtering by status and city can use standard B-tree indexes.
Flatten JSON Arrays with JSON_TABLE
When payload contains arrays, JSON_TABLE is useful for relational querying.
This turns JSON arrays into tabular rows for joins and aggregation.
Retrieve JSON in Python Application Code
When application logic needs full document parsing, fetch and decode JSON.
Prefer SQL-side extraction for filtering and sorting, and app-side parsing for complex business logic.
Handle Missing Keys and Null Values
JSON documents are often non-uniform. Query defensively so missing keys do not break reporting logic.
You can also validate key existence for quality checks:
These checks are helpful in ingestion pipelines where payload shape may drift over time.
Common Pitfalls
- Filtering by JSON keys without generated-column indexes on large tables.
- Comparing numeric values as strings due to missing casts.
- Fetching entire payload when only one key is needed.
- Storing inconsistent JSON shapes and assuming all keys always exist.
- Mixing SQL extraction and app parsing without clear ownership of validation.
Summary
- Use
JSON_EXTRACT,->, and->>for targeted JSON retrieval. - Cast extracted values to correct SQL types for comparisons and math.
- Add generated columns plus indexes for frequently filtered keys.
- Use
JSON_TABLEto query array elements relationally. - Keep retrieval strategy clear between database and application layers.
Related reading
- How to retrieve the current version of a MySQL database management system DBMS?
- How to retrieve the timestamp from cassandra?
- How to return a custom object from a Spring Data JPA GROUP BY query
- How to return a custom response pojo when request body fails validations that are defined using Bean Validation/Hibernate Validator?
- How to return the inserted item in dynamoDB
- how to return updated and previous value through a stored procedure?
- How to revert the last migration?
- How to revert the last migration?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.