Learn SQL
-- advanced.json_jsonb_operators

JSON & JSONB Operators in SQL

Semi-structured inside relational — indexably

--concept

JSONB (PG) stores parsed, indexable documents: -> (object field as JSON), ->> (as text), @> (contains), ? (has key), jsonb_set/build_object for construction. GIN indexes serve containment (@>) and key probes. Relational core stays normalized; payloads carry variable shapes. Validate with CHECK (payload ? 'email') plus generated columns for hot fields.

--how it works
1

Extract (->/->>)

-> keeps JSON, ->> yields text — project fields without fetching whole docs.

2

Contain (@>) + GIN

Containment probes seek GIN indexes — admin-role scans without full reads.

3

Build + update

jsonb_build_object/jsonb_set construct and patch — partial updates without rewrite-everything.

4

Promote hot fields

Generated columns + B-tree for hot JSON keys queried like columns.

--visualization
@> seeks GIN — ->> projects text; hot keys promoted to generated columns
emailhow
ada@x.comGIN seek @> role=admin
Stage 1 / 1
CHECK (payload ? keys) enforces shape without full schemas.
--example
query.sql
1SELECT payload->>'email' AS email FROM users WHERE payload @> '{"role":"admin"}';
Result
emailhow
ada@x.comadmin match via GIN

Containment seeks the GIN index to admin payloads only; ->> projects the email text. Fully-variable schemas stay queryable without EAV contortions.

--variants

Admin containment seek

GIN finds admin payloads only.

query.sql
1SELECT payload->>'email' FROM users WHERE payload @> '{"role":"admin"}';
Result
emailhow
ada@x.comGIN seek
1@> probes the GIN posting list.
2->> projects email text per hit.
3Seq-scan twin would parse every payload.

Hot key promotion

JSON agility, column speed.

query.sql
1email TEXT GENERATED ALWAYS AS (payload->>'email') STORED;
2CREATE INDEX ON users (email);
Result
artifacteffect
generated emailindexed like column
1Write path maintains the column.
2Reads use plain B-tree seeks.
3Normalize fully when queried like a column.
--key facts
-> vs ->>JSON vs text extraction
@> + GINcontainment seeks
Generated colshot JSON fields as indexed columns
--use cases
Variable payloads: prefs, metadata, event props
Mixed workloads: relational core + JSON edges
--under the hood
JSONB parsed + toasted — larger than columns; GIN posting lists serve @>/? efficiently.
CHECK (payload ? & array[required]) enforces shape without full schemas.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.