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

# PostgreSQL to_jsonb() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `to_jsonb()` function to convert an SQL value to a value of `JSONB` type.

## Introduction to the PostgreSQL to_jsonb() function

The `to_jsonb()` function allows you to convert an SQL value to a `JSONB` value.

Here's the syntax of the `to_jsonb()` function:

```sql
to_jsonb ( value ) → jsonb
```

In this syntax, you specify an SQL value that you want to convert to a `JSONB` value.

The `to_jsonb()` function returns a value converted to a `JSONB` value. If the value is an array or a composite value, the function will convert to arrays or objects in JSON.

## PostgreSQL to_jsonb() function examples

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

### 1) Converting a text string to a JSONB value

The following example uses the `to_jsonb()` function to convert a text string into a `JSONB` value:

```sql
SELECT to_jsonb('Hello'::text);
```

Output:

```text
 to_jsonb
----------
 "Hello"
(1 row)
```

The "Hello" is a `JSONB` value.

To verify it, you can pass the result of the `to_jsonb()` function to the `jsonb_typeof()` function.

The `jsonb_typeof()` function returns the type of a top-level JSON value as a text string.

For example:

```sql
SELECT
  JSONB_TYPEOF(
    to_jsonb('Hello' :: text)
  );
```

Output:

```text
 jsonb_typeof
--------------
 string
(1 row)
```

### 2) Converting numbers to a JSONB values

The following example uses the `to_jsonb()` function to convert numbers to `JSONB` values:

```sql
SELECT
  to_jsonb(10 :: int),
  to_jsonb(9.99 :: numeric);
```

Output:

```text
 to_jsonb | to_jsonb
----------+----------
 10       | 9.99
(1 row)
```

### 3) Converting bool values to a JSONB values

The following example uses the `to_jsonb()` function to convert boolean values to `JSONB` values:

```sql
SELECT
  to_jsonb(true :: bool),
  to_jsonb(false :: bool);
```

Output:

```text
 to_jsonb | to_jsonb
----------+----------
 true     | false
(1 row)
```

### 4) Converting NULL to a JSONB value

The following example uses the `to_jsonb()` function to convert `NULL` to a `JSONB` value:

```sql
SELECT
  to_jsonb(NULL::text);
```

Output:

```text
 to_jsonb
----------
 null
(1 row)
```

### 5) Converting a PostgreSQL array into a JSON array

The following example uses the `to_jsonb()` function to convert an array in PostgreSQL to a JSON array with the `JSONB` type:

```sql
SELECT
  to_jsonb(
    ARRAY[ 'red', 'green', 'blue' ]
  ) AS jsonb_array;
```

Output:

```text
       jsonb_array
--------------------------
 ["red", "green", "blue"]
(1 row)
```

### 6) Using the to_jsonb() function with table data

We'll use the `to_jsonb()` function to convert data in the `film` table from the [sample database](../postgresql-getting-started/postgresql-sample-database) to `JSONB` values:

```sql
SELECT
  to_jsonb(title),
  to_jsonb(length)
FROM
  film
ORDER BY
  title;
```

Output:

```
           to_jsonb            | to_jsonb
-------------------------------+----------
 "Academy Dinosaur"            | 86
 "Ace Goldfinger"              | 48
 "Adaptation Holes"            | 50
...
```

## Summary

- Use the PostgreSQL `to_jsonb()` function to convert an SQL value to a `JSONB` value.

---

## 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_build_object](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_build_object)
- [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)
