{
    "slug": "db_json_columns",
    "term": "JSON Columns in MySQL & PostgreSQL",
    "category": "database",
    "difficulty": "intermediate",
    "short": "Native JSON column types allow storing and querying semi-structured data within a relational database — without sacrificing ACID guarantees or the ability to index specific JSON paths.",
    "long": "MySQL 5.7+ and PostgreSQL 9.4+ (jsonb) support native JSON storage. PostgreSQL's jsonb stores binary-parsed JSON enabling GIN indexes on any path. MySQL's JSON type provides path operators (->>, JSON_EXTRACT) and generated columns for indexing. JSON columns are useful for variable attributes, configuration, and metadata — but abusing them to avoid schema design produces an unmaintainable document store inside a relational database.",
    "aliases": [
        "JSONB",
        "JSON type",
        "JSON column"
    ],
    "tags": [
        "database",
        "postgresql",
        "mysql",
        "json"
    ],
    "misconception": "JSON columns replace proper schema design — they are an escape hatch for genuinely variable data, not a substitute for normalised tables when the structure is known.",
    "why_it_matters": "Storing all user preferences or metadata as JSON in one column avoids dozens of migration files — but querying and indexing specific JSON fields requires understanding the database's JSON operators.",
    "common_mistakes": [
        "Using JSON for data that has a fixed, known structure — a proper column is faster, indexable, and type-safe.",
        "Not creating a GIN index (PostgreSQL) or generated column index (MySQL) on frequently queried JSON paths.",
        "Querying JSON with LIKE '%value%' — always use the database's native JSON path operators.",
        "Mixing jsonb (binary, fast) and json (text, preserves whitespace) in PostgreSQL without understanding the difference."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "db_composite_indexes",
        "database_indexing",
        "db_schema_design",
        "db_document_stores"
    ],
    "prerequisites": [
        "db_schema_design",
        "query_optimisation",
        "data_normalisation"
    ],
    "refs": [
        "https://www.postgresql.org/docs/current/datatype-json.html",
        "https://dev.mysql.com/doc/refman/8.0/en/json.html"
    ],
    "bad_code": "-- Querying JSON with LIKE — full table scan, no index:\nSELECT * FROM users WHERE metadata LIKE '%\"role\":\"admin\"%';\n\n-- MySQL: JSON_EXTRACT without generated column — not indexed:\nSELECT * FROM users WHERE JSON_EXTRACT(metadata, '$.role') = 'admin';",
    "good_code": "-- PostgreSQL: GIN index on jsonb + path operator:\nCREATE INDEX idx_users_metadata ON users USING GIN (metadata);\nSELECT * FROM users WHERE metadata @> '{\"role\": \"admin\"}';\n\n-- MySQL: generated column + index:\nALTER TABLE users\n    ADD COLUMN role VARCHAR(50) GENERATED ALWAYS AS (metadata->>'$.role') STORED,\n    ADD INDEX idx_users_role (role);",
    "quick_fix": "Use JSON columns for truly variable/optional attributes — but add generated columns and indexes for any JSON field you query; never store data in JSON that you need to filter, join, or aggregate",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-04-19",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/db_json_columns",
        "html_url": "https://codeclaritylab.com/glossary/db_json_columns",
        "json_url": "https://codeclaritylab.com/glossary/db_json_columns.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[JSON Columns in MySQL & PostgreSQL](https://codeclaritylab.com/glossary/db_json_columns) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/db_json_columns"
            }
        }
    }
}