PostgreSQL has two column types for JSON. They accept the same input but store it very differently.
The short version
json | jsonb | |
|---|---|---|
| Stored as | Exact input text | Parsed binary |
| Whitespace | Preserved | Removed |
| Key order | Preserved | Not preserved |
| Duplicate keys | All kept | Last one wins |
| Insert speed | Faster (just validates) | Slightly slower (parses and converts) |
| Query speed | Re-parses on every access | Fast |
| Containment / existence operators | No | @>, <@, ?, ?|, ?& |
| GIN indexing | No | Yes |
| Equality comparison | No | Yes |
Use jsonb unless you need the exact original text.
See the difference
SELECT '{"b": 1, "a": 2, "a": 3}'::json AS j,
'{"b": 1, "a": 2, "a": 3}'::jsonb AS jb;
j | {"b": 1, "a": 2, "a": 3}
jb | {"a": 3, "b": 1}
json returns the text unchanged. jsonb drops whitespace, reorders keys and keeps only the last "a".
A jsonb table
CREATE TABLE employees (
id bigint PRIMARY KEY,
data jsonb NOT NULL
);
INSERT INTO employees VALUES
(1001, '{"name": "Asha Rao", "department": "Engineering",
"skills": ["go", "kubernetes"], "address": {"city": "Bengaluru"}}'),
(1002, '{"name": "Daniel Okafor", "department": "Engineering",
"skills": ["typescript"], "address": {"city": "Lagos"}}');
Querying
-- Read a field as text
SELECT data->>'name' FROM employees;
-- Navigate nested objects
SELECT data->'address'->>'city' FROM employees;
SELECT data #>> '{address,city}' FROM employees;
-- Containment: employees in Engineering who know Go
SELECT id FROM employees
WHERE data @> '{"department": "Engineering", "skills": ["go"]}';
-- Key existence
SELECT id FROM employees WHERE data ? 'manager';
-- SQL/JSON path
SELECT jsonb_path_query(data, '$.skills[*]') FROM employees;
Updating
-- Set one nested field
UPDATE employees
SET data = jsonb_set(data, '{address,city}', '"Mumbai"')
WHERE id = 1001;
-- Merge fields (right side wins)
UPDATE employees SET data = data || '{"isActive": true}';
-- Remove a key
UPDATE employees SET data = data - 'manager';
Indexing
-- Whole-document GIN index: speeds up @>, ?, ?|, ?&
CREATE INDEX employees_data_gin ON employees USING gin (data);
-- Smaller GIN index that only supports @> (and jsonpath @? / @@)
CREATE INDEX employees_data_path ON employees USING gin (data jsonb_path_ops);
-- One hot field: a plain B-tree expression index
CREATE INDEX employees_department ON employees ((data->>'department'));
Filters must use the same operator or expression the index was built for. WHERE data->>'department' = 'Engineering' uses the expression index, not the GIN index.
When json is the right choice
- You store raw webhook or API payloads and must reproduce them byte for byte, for example to verify a signature.
- You only ever store and return the document and never query inside it.
Design tips
- Put fields you filter, join or sort on in real columns; keep jsonb for the flexible remainder.
- Add a
CHECKconstraint (or validate with JSON Schema in the application) so bad shapes do not sneak in. - Very large jsonb values are TOASTed and compressed; updating one key rewrites the whole value, so avoid huge, frequently updated documents.
Related
- JSON Schema guide for validating documents before they reach the database.
- JSON vs JSONL for bulk loading with
COPYor import tools.