> This page location: JSON Functions > jsonb_build_object
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL jsonb_build_object() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `jsonb_build_object()` function to create a JSON object based on a variadic argument list.

## Introduction to the PostgreSQL jsonb_build_object() function

The `jsonb_build_object()` function allows you to build an object out of a variadic argument list.

Here's the basic syntax of the `jsonb_build_object()` function:

```sql
jsonb_build_object ( VARIADIC "any" ) → jsonb
```

In this syntax, you pass a list of alternative keys and values. The list must have an even number of elements or you'll get an error.

The function will coerce the key to text and value to a JSON value using the `to_jsonb()` function.

The `jsonb_build_object()` function returns a JSON object with the specified key and values.

## PostgreSQL jsonb_build_object() function examples

Let's explore some examples of using the PostgreSQL `jsonb_build_object()` function.

### 1) Basic jsonb_build_object() function example

The following example uses the `jsonb_build_object()` function to build an object out of the alternating keys and values:

```sql
SELECT
  jsonb_build_object(
    'title', 'Academy Dinosaur', 'length',
    86
  );
```

Output:

```text
             jsonb_build_object
---------------------------------------------
 {"title": "Academy Dinosaur", "length": 86}
(1 row)
```

### 2) Using jsonb_build_object() function with table data example

The following example uses the `jsonb_build_object()` function to create a JSON object based on the title and length of films in the `film` table from the [sample database](../postgresql-getting-started/postgresql-sample-database):

```sql
SELECT
  jsonb_build_object('title', title, 'length', length)
FROM
  film
ORDER BY
  length DESC;
```

Output:

```text
                   jsonb_build_object
---------------------------------------------------------
 {"title": "Muscle Bright", "length": 185}
 {"title": "Control Anthem", "length": 185}
 {"title": "Sweet Brotherhood", "length": 185}
...
```

### 3) Using the jsonb_build_object() function with an odd number of values

The following example attempts to use the `jsonb_build_object()` function with an odd number of values:

```sql
SELECT
  jsonb_build_object(
    'title', 'Theory Mermaid', 'length'
  );
```

It returns the following error:

```
ERROR:  argument list must have even number of elements
HINT:  The arguments of jsonb_build_object() must consist of alternating keys and values.
```

## Summary

- Use the PostgreSQL `jsonb_build_object()` function to build a JSON object out of a variadic argument list.

---

## Related docs (JSON Functions)

- [Extracting JSON Data](https://neon.com/postgresql/postgresql-json-functions/postgresql-json-extract)
- [JSONB Operators](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb-operators)
- [jsonb_agg](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_agg)
- [jsonb_array_elements](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_elements)
- [jsonb_array_elements_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_elements_text)
- [jsonb_array_length](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_length)
- [jsonb_build_array](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_build_array)
- [jsonb_each](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_each)
- [jsonb_each_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_each_text)
- [jsonb_extract_path](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_extract_path)
- [jsonb_extract_path_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_extract_path_text)
- [jsonb_insert](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_insert)
- [jsonb_object](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object)
- [jsonb_object_agg](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object_agg)
- [jsonb_object_keys](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object_keys)
- [JSON Path](https://neon.com/postgresql/postgresql-json-functions/postgresql-json-path)
- [jsonb_path_exists](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_exists)
- [jsonb_path_query](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query)
- [jsonb_path_query_array](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query_array)
- [jsonb_path_query_first](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query_first)
- [jsonb_pretty](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_pretty)
- [jsonb_set](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_set)
- [jsonb_strip_nulls](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_strip_nulls)
- [jsonb_to_record](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_to_record)
- [jsonb_populate_record](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_populate_record)
- [jsonb_populate_recordset](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_populate_recordset)
- [jsonb_typeof](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_typeof)
- [row_to_json](https://neon.com/postgresql/postgresql-json-functions/postgresql-row_to_json)
- [to_jsonb](https://neon.com/postgresql/postgresql-json-functions/postgresql-to_jsonb)
