JSONB (binary JSON)
Last updated: 2026-11-04
JSONB (binary JSON)
JSONB is PostgreSQL's binary JSON type. It parses the document, stores it reordered by key length and byte order, and keeps only the last of any duplicate keys, which is what makes GIN indexing and containment queries possible. Four access operators matter most: -> yields a JSONB value, ->> yields text, #> walks a path to a JSONB value and #>> walks a path to text, while @> tests containment. The price is that whitespace and key order are rewritten, so it is the wrong choice when the original JSON text must be preserved.
| Kind | type |
|---|---|
| Example Language | SQL |
📝 Syntax
JSONB —— binary JSON
col -> 'key' -- value as JSONB
col ->> 'key' -- value as TEXT
col #> '{a,b}' -- path to a JSONB value
col #>> '{a,b}' -- path to TEXT
col @> '{"a":1}' -- containment test
Returns:Stores a parsed JSON document. -> and #> return JSONB, which can take part in further jsonb operations; ->> and #>> return TEXT, ready for string comparison; @> returns a boolean.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
id | arrow_jsonb | arrow_text | contains | arrow_type
----+-------------+------------+----------+------------
1 | "Alice" | Alice | t | jsonb
2 | "Bob" | Bob | t | jsonb
📌 Key Points
- -> returns JSONB, so the value renders with quotes as "Alice"; ->> returns TEXT and yields plain Alice, ready for string comparison.
- @> is the containment test: doc @> '{"tags":["pg"]}' is true when the document contains that substructure. Array containment means "contains all of these elements", not "contains any".
- JSONB reorders keys, drops duplicate keys and squeezes whitespace, so the original formatting cannot be preserved. Use the JSON type when the exact text matters, but JSON supports neither @> queries nor GIN indexes.