json_object()
Constructs a JSON object of all the key/value pairs given, or an empty object if none are given. key_expression is a scalar expression defining the JSON key, which is converted to the text type. It cannot be NULL nor can it belong to a type that has a cast to the json type. If WITH UNIQUE KEYS is specified, there must not be any duplicate key_expression. Any pair for which the value_expression evaluates to NULL is omitted from the output if ABSENT ON NULL is specified; if NULL ON NULL is specified or the clause omitted, the key is included with value NULL.
Signature
json_object ( [ { key_expression { VALUE | ':' } value_expression [ FORMAT JSON [ ENCODING UTF8 ] ] }[, ...] ] [ { NULL | ABSENT } ON NULL ] [ { WITH | WITHOUT } UNIQUE [ KEYS ] ] [ RETURNING data_type [ FORMAT JSON [ ENCODING UTF8 ] ] ])
json_object ( text[] ) → json
json_object ( keys text[], values text[] ) → jsonExamples
SELECT json_object('code' VALUE 'P123', 'title': 'Jaws');{"code" : "P123", "title" : "Jaws"}SELECT json_object('{a, 1, b, "def", c, 3.5}');{"a" : "1", "b" : "def", "c" : "3.5"}SELECT json_object('{{a, 1}, {b, "def"}, {c, 3.5}}');{"a" : "1", "b" : "def", "c" : "3.5"}Run against PostgreSQL 18.6 on 2026-09-13. This is the output we got, not the documentation’s.
Want to run this against your own PostgreSQL server from an iPhone, iPad or Mac? These are ours — every connection goes straight from the device to your database, with no account and no server of ours in between.
Signature and description adapted from the PostgreSQL manual, used under the PostgreSQL License. PostgreSQL is a registered trademark of the PostgreSQL Community Association of Canada. This page is not affiliated with the PostgreSQL Global Development Group.