JSONB extraction operator ->

Last updated: 2026-11-04

JSONB extraction operator ->

-> extracts a sub-value from jsonb by key or array index and returns jsonb itself, so calls can be chained. A missing key yields NULL rather than an error. It reaches into nested structures in one go, for example doc -> 'address' -> 'city'.

Kindoperator
Example LanguageSQL

📝 Syntax

jsonb -> text        -- extract by key
jsonb -> integer     -- extract by array index
jsonb ->> text       -- extract by key, returned as text

Precedence: -> and ->> fall in the "any other operator" tier, below `* / %` and `+ -` and above the comparison operators. So `doc -> 'a' = '1'::jsonb` extracts first, then compares; with ->> remember the result is text, so write `doc ->> 'name' = 'Ada'` rather than comparing against a jsonb literal.

⚙️ Parameters

jsonbThe source jsonb value; if NULL the result is NULL.
keyA text key; NULL when the key does not exist.
indexAn integer array index starting at 0; NULL when out of range or not an array.

Returns:The sub-value as jsonb; NULL when the key or index does not exist.

▶ 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:

SELECT id, pg_typeof(doc -> 'name') AS value_type, (doc -> 'name')::text AS value_text FROM t_jsonb
id | value_type | value_text
---+------------+-----------
 1 | jsonb      | "Ada"     
(1 row)

SELECT id, (doc -> 'address')::text AS address_jsonb, (doc -> 'address' -> 'city')::text AS city_jsonb FROM t_jsonb
id | address_jsonb       | city_jsonb
---+---------------------+-----------
 1 | {"city": "Beijing"} | "Beijing" 
(1 row)

SELECT id, (doc -> 'tags' -> 0)::text AS first_tag FROM t_jsonb
id | first_tag
---+----------
 1 | "db"     
(1 row)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QDoes -> raise an error when the key is missing?
ANo, it returns NULL. To tell "key absent" from "key present but null", use `doc ? 'key'` to test for key existence.
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%

🙏 帮我们做得更好

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

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