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

# PostgreSQL jsonb_object_keys() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `jsonb_object_keys()` function to extract the keys from a JSON object.

## Introduction to the PostgreSQL jsonb_object_keys() function

The `jsonb_object_keys()` function allows you to extract the keys of a [JSON](../postgresql-tutorial/postgresql-json) object into a set of text values.

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

```sql
jsonb_object_keys(json_object)
```

In this syntax:

- `json_object` is the JSON object of type JSONB that you want to extract the keys.

The `jsonb_object_keys()` function returns a set of text values representing the keys in the `json_object`.

If the `json_object` is not a JSON object, the `jsonb_object_keys()` function will issue an error.

If the `json_object` is `NULL`, the function will return an empty set.

## PostgreSQL jsonb_object_keys() function examples

Let's take some examples of using the `jsonb_object_keys()` function.

### 1) Basic the jsonb_object_keys() function examples

The following example uses the `jsonb_object_keys()` function to extract the keys of a JSON object as a set of text values:

```sql
SELECT
  jsonb_object_keys(
    '{"name": "Joe", "age": 18, "city": "New York"}'
  );
```

Output:

```text
 jsonb_object_keys
-------------------
 age
 city
 name
(3 rows)
```

### 2) Using the PostgreSQL jsonb_object_keys() function with table data

First, [create a table](../postgresql-tutorial/postgresql-create-table) called `person`:

```sql
CREATE TABLE person (
    id SERIAL PRIMARY KEY,
    info JSONB
);
```

In the `person` table, the `info` column has the type JSONB that stores various information about each person.

Second, [insert rows](../postgresql-tutorial/postgresql-insert-multiple-rows) into the `person` table:

```sql
INSERT INTO person (info)
VALUES
    ('{"name": "John", "age": 30, "city": "New York"}'),
    ('{"name": "Alice", "city": "Los Angeles"}'),
    ('{"name": "Bob", "age": 35 }');
```

Third, get the keys of the objects in the `info` column:

```sql
SELECT jsonb_object_keys(info)
FROM person;
```

Output:

```text
 jsonb_object_keys
-------------------
 age
 city
 name
 city
 name
 age
 name
(7 rows)
```

To get unique keys from all the stored JSON objects in the info column, you can use the `DISTINCT` operator:

```sql
SELECT DISTINCT jsonb_object_keys(info)
FROM person;
```

Output:

```text
 jsonb_object_keys
-------------------
 age
 city
 name
(3 rows)
```

### 3) Dynamically accessing keys

The following example shows how to dynamically access values corresponding to each key retrieved using `jsonb_object_keys()`:

```sql
SELECT
    id,
    key,
    info->key AS value
FROM
    person,
    jsonb_object_keys(info) AS key;

```

Output:

```
 id | key  |     value
----+------+---------------
  1 | age  | 30
  1 | city | "New York"
  1 | name | "John"
  2 | city | "Los Angeles"
  2 | name | "Alice"
  3 | age  | 35
  3 | name | "Bob"
(7 rows)
```

In this example, the query returns each key along with its corresponding value from the `info` column of the `person` table.

## Summary

- Use the `jsonb_object_keys()` function to extract the keys from a JSON object.

---

## 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)
- [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)
