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'.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 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
| jsonb | The source jsonb value; if NULL the result is NULL. |
|---|---|
| key | A text key; NULL when the key does not exist. |
| index | An 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
- -> returns jsonb while ->> returns text — that is the essential difference between them.
- A missing key or out-of-range index yields NULL instead of an error.
- -> chains: doc -> 'address' -> 'city'.
- Using an integer index on a non-array or a string key on a non-object returns NULL.
💡 Related Cases
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.