JSONB text extraction operator ->>
Last updated: 2026-11-04
JSONB text extraction operator ->>
->> differs from -> in exactly one way, and it matters: ->> returns text instead of jsonb. Once a scalar is extracted you can compare it directly against string literals and cast it, for example (doc ->> 'age')::int. A missing key still yields NULL.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
jsonb ->> text -- extract by key, returned as text
jsonb ->> integer -- extract by array index, returned as text
Precedence: ->> is in the "any other operator" tier, below arithmetic and above comparison. Because it returns text, the left side of a comparison is usually a string: `doc ->> 'name' = 'Ada'`. The -> operator returns jsonb, so compare it against a jsonb literal: `doc -> 'name' = '"Ada"'::jsonb`.
⚙️ 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 text (plain text for scalars, JSON text for objects and arrays); 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' AS value_text FROM t_jsonb_text
id | value_type | value_text
---+------------+-----------
1 | text | Ada
(1 row)
SELECT id, doc ->> 'address' AS address_text, doc ->> 'age' AS age_text, (doc ->> 'age')::int AS age_int FROM t_jsonb_text
id | address_text | age_text | age_int
---+---------------------+----------+--------
1 | {"city": "Beijing"} | 36 | 36
(1 row)
SELECT id, doc ->> 'missing' AS missing_text FROM t_jsonb_text
id | missing_text
---+-------------
1 | NULL
(1 row)
📌 Key Points
- ->> returns text while -> returns jsonb — that is the only difference.
- When the extracted value is an object or an array, ->> returns its JSON text.
- NULL semantics match ->: a missing key yields NULL.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhen should I use ->> instead of ->?
AUse ->> when you want the value as ordinary text or a number: it compares directly and casts with ::int. Use -> when you need to drill further (chained access) or run jsonb operations.