Use jsonb. It gets containment (@>), key-existence
(?), path queries, equality, and GIN indexing; json has none of those and
exists to store your input byte-for-byte. Pick json only when exact text matters — key
order, duplicate keys, whitespace. The PostgreSQL docs say the
same: prefer jsonb unless you have “legacy assumptions about ordering of object keys”.
Table of Contents
What each type does to your input
One table, both column types, one deliberately ugly payload — duplicate key, odd spacing:
CREATE TABLE events (id serial PRIMARY KEY, doc_json json, doc_jsonb jsonb);
INSERT INTO events (doc_json, doc_jsonb) VALUES
('{"z": 1, "a": 2, "z": 3}',
'{"z": 1, "a": 2, "z": 3}');
SELECT doc_json, doc_jsonb FROM events;
doc_json | doc_jsonb
----------------------------+------------------
{"z": 1, "a": 2, "z": 3} | {"a": 2, "z": 3}
json stored my text verbatim: the duplicate "z", the triple space, the
original key order. jsonb parsed it once into a binary form — keys sorted, whitespace
gone, and only the last duplicate kept. That normalization is the entire trade: it destroys
information about the input text, and it is what makes every operator below possible. Note the
duplicate buys json less than it seems — extraction still resolves to the last value on
both types:
SELECT doc_json->>'z' AS from_json, doc_jsonb->>'z' AS from_jsonb FROM events;
from_json | from_jsonb
-----------+------------
3 | 3
The operators only jsonb has
Containment on the json column is not slower — it does not exist:
SELECT doc_json @> '{"a": 2}' FROM events;
ERROR: operator does not exist: json @> unknown
Neither does equality (doc_json = doc_json fails the same way), which also rules out
DISTINCT and GROUP BY on the column. On jsonb, all of it works,
including SQL/JSON path queries:
SELECT doc_jsonb @> '{"a": 2}' AS contains, doc_jsonb ? 'a' AS has_key FROM events;
contains | has_key
----------+---------
t | t
SELECT jsonb_path_query(doc_jsonb, '$.* ? (@ > 1)') FROM events;
jsonb_path_query
------------------
2
3
Indexing: the part that decides it
json cannot even hold a GIN index:
CREATE INDEX docs_json_gin ON docs USING GIN (body_json);
ERROR: data type json has no default operator class for access method "gin"
So I built a 200,000-row table with the same generated document in both a json and a
jsonb column, indexed only the jsonb one, and ran the same containment
search against each. The jsonb query walks the index:
EXPLAIN ANALYZE SELECT count(*) FROM docs
WHERE body_jsonb @> '{"tag": "blue", "score": 42}';
Aggregate (cost=132.42..132.43 rows=1 width=8) (actual time=2.674..2.674 rows=1 loops=1)
-> Bitmap Heap Scan on docs (cost=56.12..132.37 rows=20 width=0) (actual time=1.703..2.438 rows=516 loops=1)
Recheck Cond: (body_jsonb @> '{"tag": "blue", "score": 42}'::jsonb)
Heap Blocks: exact=516
-> Bitmap Index Scan on docs_jsonb_gin (cost=0.00..56.11 rows=20 width=0) (actual time=1.667..1.667 rows=516 loops=1)
Index Cond: (body_jsonb @> '{"tag": "blue", "score": 42}'::jsonb)
Planning Time: 0.244 ms
Execution Time: 2.702 ms
The json version has to cast every row to jsonb just to ask the
question, then reads the whole table:
EXPLAIN ANALYZE SELECT count(*) FROM docs
WHERE body_json::jsonb @> '{"tag": "blue", "score": 42}';
Finalize Aggregate (cost=6950.63..6950.64 rows=1 width=8) (actual time=43.079..44.216 rows=1 loops=1)
-> Gather (cost=6950.42..6950.63 rows=2 width=8) (actual time=43.018..44.212 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=5950.42..5950.43 rows=1 width=8) (actual time=41.321..41.322 rows=1 loops=3)
-> Parallel Seq Scan on docs (cost=0.00..5948.33 rows=833 width=0) (actual time=0.178..41.310 rows=172 loops=3)
Filter: ((body_json)::jsonb @> '{"tag": "blue", "score": 42}'::jsonb)
Rows Removed by Filter: 66495
Planning Time: 0.197 ms
Execution Time: 44.260 ms
2.702 ms against 44.260 ms at 200k rows, and the gap grows with the table. The seq scan also re-parses the stored text on every row — the reparse cost the docs warn about — which the index-backed query never pays.
Storage: the one place json wins
I expected pretty-printed input to flip this in jsonb‘s favor. It did not —
jsonb‘s binary form carries enough per-value overhead that on documents this size,
json stayed smaller even with the whitespace it must keep:
SELECT payload, pg_column_size(payload::json) AS json_bytes,
pg_column_size(payload::jsonb) AS jsonb_bytes
FROM (VALUES ('{"tag":"blue","score":42}'),
(jsonb_pretty('{"tag":"blue","score":42}'::jsonb))) v(payload);
payload | json_bytes | jsonb_bytes
---------------------------+------------+-------------
{"tag":"blue","score":42} | 29 | 44
{ +| 42 | 44
"tag": "blue", +| |
"score": 42 +| |
} | |
Across the 200k-row table the totals were 11 MB of json against 17 MB of
jsonb for identical documents. If a few extra bytes per row mattered more than being
able to index or even compare the column, I would not be storing JSON in the first place — so this
does not change the verdict, but it is real and worth knowing before you convert a huge table.
Verified 2026-08-16 on PostgreSQL 17.11 (aarch64-apple-darwin25.6.0). Every command above was executed on a scratch database and the output pasted unedited, including all three errors. Reproduce all of it with this script, which builds a throwaway cluster in a temp directory, checks each claim, and deletes it afterwards.