Part 14 / Topic 2 of 10
JSON vs JSONB
Postgres has two JSON types, and the name tells the story:
jsonstores the exact text you inserted — whitespace, key order, duplicate keys and all. Validated on the way in, re-parsed on every use.jsonbstores 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.
- Which type preserves key order and duplicates?
- Why is jsonb usually preferred anyway?
Show the answers
json— it stores the literal text.- 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.