PostgreSQL 18 reference · JSON Functions

PostgreSQL TO_JSONB() Function

Convert any SQL value, including a whole row, to jsonb. Rows become objects with column names as keys.

PostgreSQL 18 Since 9.5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

to_jsonb(anyelement)

Parameters

ParameterDescription
anyelement Any value, row or array.

Return value

jsonb.

Examples

A row as JSON
SELECT to_jsonb(c) AS row_as_json
FROM categories AS c
WHERE category_id = 1;

Output

             row_as_json
-------------------------------------
 {"name": "Books", "category_id": 1}
(1 row)

Run this on your computer — see “Get Started” for setup.

Arrays and text
SELECT to_jsonb(ARRAY[1, 2]) AS arr, to_jsonb('he said "hi"'::text) AS str;

Output

  arr   |       str
--------+------------------
 [1, 2] | "he said \"hi\""
(1 row)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.5Added

Was this page helpful?