Lab 17: PostgreSQL JSONB Advanced
Step 1 — JSON vs JSONB
-- JSON: stores exact text (preserves whitespace, duplicate keys)
-- JSONB: binary format, deduplicates keys, supports indexing
CREATE TABLE comparison (
id SERIAL PRIMARY KEY,
raw_json JSON,
bin_json JSONB
);
INSERT INTO comparison (raw_json, bin_json) VALUES (
'{"name": "Alice", "age": 28, "name": "Alice Updated"}', -- duplicate key
'{"name": "Alice", "age": 28, "name": "Alice Updated"}'
);
-- JSON preserves duplicate keys and whitespace
SELECT raw_json FROM comparison;
-- {"name": "Alice", "age": 28, "name": "Alice Updated"}
-- JSONB deduplicates (last value wins) and normalizes
SELECT bin_json FROM comparison;
-- {"age": 28, "name": "Alice Updated"}Step 2 — Setup: User Profiles with JSONB
Step 3 — Navigation Operators: -> and ->>
Step 4 — Containment and Existence Operators
Step 5 — Modifying JSONB: jsonb_set, jsonb_insert
Step 6 — GIN Index and Query Performance
Step 7 — jsonb_path_query and JSONB Aggregation
Step 8 — Capstone: Product Catalog with JSONB Specs
Summary
Operator
Input
Output
Use
Last updated
