to_jsonb(anyelement) — Convert a value into JSONB

Last updated: 2026-11-04

to_jsonb(anyelement) — Convert a value into JSONB

to_jsonb() converts any SQL value into JSONB, and a whole row can be converted directly to get a JSON object holding every column. It is the usual way to export relational data as JSON for an API response, or to gather several rows into a single JSON array.

Kindfunction
Example LanguageSQL

📝 Syntax

to_jsonb(value anyelement) → jsonb
row_to_json(record [, pretty_bool]) → json

⚙️ Parameters

value anyelementThe SQL value to convert; pass a table alias to convert an entire row at once.

Returns:jsonb — the converted value. Numbers, booleans, text, arrays and nested objects all map correctly.

▶ 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 | product_json
----+--------------------------------------------------------------------
  1 | {"id": 1, "name": "USB-C Hub", "price": 45.00, "in_stock": true}
  2 | {"id": 2, "name": "HDMI Cable", "price": 12.50, "in_stock": false}
(2 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QHow does to_jsonb() differ from to_json()?
Ato_json() returns json text preserving the original whitespace and key order, while to_jsonb() returns parsed binary JSONB — smaller, indexable and usable with the -> operators.
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%

🙏 帮我们做得更好

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

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