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

# PostgreSQL jsonb_each() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `jsonb_each()` function to expand a JSON object into a set of key/value pairs.

## Introduction to the PostgreSQL jsonb_each() function

The `jsonb_each()` function allows you to expand a top-level [JSON](../postgresql-tutorial/postgresql-json) object of a JSONB value into a set of key/value pairs. The keys are text and values are JSON values.

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

```sql
jsonb_each(json_object)
```

In this syntax:

- `json_object` is the JSON object that you want to expand the key/value pairs.

The function returns a set of records where each record consists of two fields key of type `text` and value of the `JSONB`.

If the `json_object` is not a JSON object, the function will issue an error. In case the `json_object` is null, the function returns an empty set.

## PostgreSQL jsonb_each() function examples

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

### 1) Basic PostgreSQL jsonb_each() function example

The following example uses the `jsonb_each` function to expand the key/value pair of a JSON object:

```sql
SELECT
  *
FROM
  jsonb_each(
    '{"name": "John", "age": 30, "city": "New York"}'
  );
```

Output:

```text
 key  |   value
------+------------
 age  | 30
 city | "New York"
 name | "John"
(3 rows)
```

If you want to retrieve a particular key, you can filter the key in the `WHERE` clause.

For example, the following statement returns the name and age of the object:

```sql
SELECT
  *
FROM
  jsonb_each(
    '{"name": "John", "age": 30, "city": "New York"}'
  )
WHERE
  key in ('name', 'age');
```

Output:

```text
 key  | value
------+--------
 age  | 30
 name | "John"
(2 rows)
```

### 2) Using the jsonb_each() function with table data

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

```sql
CREATE TABLE links (
    id SERIAL PRIMARY KEY,
    href TEXT NOT NULL,
    attributes JSONB
);
```

In the `links` table, the `attributes` column has the type of `JSONB` that stores various attributes of a link.

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

```sql
INSERT INTO links (href, attributes)
VALUES
    ('https://example.com', '{"rel": "stylesheet", "type": "text/css", "media": "screen"}'),
    ('https://example.org', '{"rel": "icon", "type": "image/x-icon"}'),
    ('https://example.net', '{"rel": "alternate", "type": "application/rss+xml", "title": "RSS Feed"}');
```

Third, expand the key/value pairs of the objects in `attributes` column into a set of key/value pairs using the `jsonb_each()` function:

```sql
SELECT
  href,
  key,
  value
FROM
  links,
  jsonb_each(attributes);
```

Output:

```text
        href         |  key  |         value
---------------------+-------+-----------------------
 https://example.com | rel   | "stylesheet"
 https://example.com | type  | "text/css"
 https://example.com | media | "screen"
 https://example.org | rel   | "icon"
 https://example.org | type  | "image/x-icon"
 https://example.net | rel   | "alternate"
 https://example.net | type  | "application/rss+xml"
 https://example.net | title | "RSS Feed"
(8 rows)
```

## Summary

- Use the `jsonb_each()` function to expand a JSON object into a set of key/value pairs.

---

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