🐘 PostgreSQL Intermediate

How does JSONB differ from JSON in PostgreSQL?

Answer

PostgreSQL supports two JSON storage types. JSON stores JSON as text, preserving the original format including whitespace and duplicate keys — it validates JSON syntax but doesn't parse deeply. JSONB stores JSON in a decomposed binary format — it parses and validates fully, removes whitespace, removes duplicate keys (last value wins), and reorders keys. Key advantages of JSONB: supports indexing (GIN indexes), has more operators, and is much faster for querying/filtering. Query operators: -> (get JSON object), ->> (get as text), #> (path access), @> (contains), <@ (contained by), ? (key exists). Always prefer JSONB over JSON unless you specifically need to preserve insertion order or duplicate keys.