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.

Kindtype
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhen to use -> versus ->>?
AUse -> when the result must take part in further jsonb operations such as nested access, @> tests or GIN index matching. Use ->> when you only need the value for display or string comparison: WHERE doc ->> 'name' = 'Alice' works directly.
QHow do you index a JSONB column?
AUse a GIN index: CREATE INDEX ON t_jsonb USING gin (doc). It accelerates @>, ?, ?| and ?&. When you always query one fixed key, an expression index on gin (doc -> 'name') is even faster.
QCan JSONB hold arrays?
AYes. JSONB holds any valid JSON value, arrays and scalars included. Use jsonb_typeof() to inspect the kind: 'object', 'array', 'string', 'number', 'boolean' or 'null'.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏