Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 14 / Topic 2 of 10

JSON vs JSONB

Postgres has two JSON types, and the name tells the story:

  • json stores the exact text you inserted — whitespace, key order, duplicate keys and all. Validated on the way in, re-parsed on every use.
  • jsonb stores a parsed binary form — duplicate keys resolved (last wins), key order not preserved, parsed once. Operators, indexing and every function family below work on jsonb.
SELECT '{"b": 1, "a": 2, "a": 3}'::json  AS json_text,
       '{"b": 1, "a": 2, "a": 3}'::jsonb AS jsonb_parsed;
        json_text         |   jsonb_parsed   
--------------------------+------------------
 {"b": 1, "a": 2, "a": 3} | {"a": 3, "b": 1}
(1 row)

The text type kept everything; jsonb kept the meaning (and the last a). Unless you are archiving byte-exact documents (signed payloads, audit requirements), jsonb is the right choice — which is why shopdb's events.payload is jsonb and why the rest of this part says jsonb throughout.

🧪 Quick quiz — answer from memory, then check.

  1. Which type preserves key order and duplicates?
  2. Why is jsonb usually preferred anyway?
Show the answers
  1. json — it stores the literal text.
  2. Stored parsed: faster to query, de-duplicated, and it's what operators and GIN indexes work on.

Done here? The practice set for Part 14 — 6 exercises with solutions — is on the part page.

Search the guide