PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_BUILD_OBJECT() Function

Build a JSON object from alternating keys and values. json_build_object() returns json instead of jsonb.

PostgreSQL 18 Since 9.5

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

Syntax

jsonb_build_object(key1, value1, ...)

Parameters

ParameterDescription
key, value Pairs; SQL NULL becomes JSON null.

Return value

jsonb.

Examples

Rows as JSON
SELECT jsonb_build_object('id', customer_id, 'name', name, 'email', email) AS customer
FROM customers
WHERE customer_id IN (4, 5);

Output

                             customer
-------------------------------------------------------------------
 {"id": 4, "name": "Linus Torvalds", "email": "[email protected]"}
 {"id": 5, "name": "Margaret Hamilton", "email": null}
(2 rows)

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

Nested values
SELECT jsonb_build_object('tags', jsonb_build_array('a', 'b'), 'count', 2) AS nested;

Output

              nested
----------------------------------
 {"tags": ["a", "b"], "count": 2}
(1 row)

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

Version history

VersionChange
PostgreSQL 9.5Added

In other languages

Was this page helpful?