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

# PostgreSQL jsonb_path_query_first() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `jsonb_path_query_first()` function to extract the first JSON value that matches a JSON path expression from a JSON document.

## Introduction to the PostgreSQL jsonb_path_query_first() function

The `jsonb_path_query_first()` function allows you to query data from a [JSONB document](../postgresql-tutorial/postgresql-json) based on a [JSON path](https://neon.com/postgresql/postgresql-json-functions/postgresql-json-path) expression and return the first match.

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

```sql
jsonb_path_query_first(jsonb_data, json_path)
```

In this syntax:

- First, specify a JSONB data from which you want to query data.
- Second, provide a JSON path to match elements in the JSONB data.

If the `jsonb_path_query_first()` function doesn't find any match, it returns `NULL`.

## PostgreSQL jsonb_path_query_first() function examples

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

### 1) Basic jsonb_path_query_first() function example

The following example uses the `jsonb_path_query_first()` function to get the first pet of a person:

```sql
SELECT jsonb_path_query_first(
    '{"name": "Alice", "pets": ["Lucy","Bella"]}',
    '$.pets[*]'
) AS first_pet_name;
```

Output:

```text
 first_pet_name
----------------
 "Lucy"
(1 row)
```

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

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

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

In the `person` table, the `data` column has the type of JSONB that stores employee information including name, age, and pets.

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

```sql
INSERT INTO person (data)
VALUES
    ('{"name": "Alice", "age": 30, "pets": [{"type": "cat", "name": "Fluffy"}, {"type": "dog", "name": "Buddy"}]}'),
    ('{"name": "Bob", "age": 35, "pets": [{"type": "dog", "name": "Max"}]}'),
    ('{"name": "Charlie", "age": 40, "pets": [{"type": "rabbit", "name": "Snowball"}]}')
RETURNING *;
```

Third, retrieve the first pet name using the `jsonb_path_query_first()` function:

```sql
SELECT jsonb_path_query_first(data, '$.pets[*].name') AS first_pet_name
FROM person;
```

Output:

```text
 first_pet_name
----------------
 "Fluffy"
 "Max"
 "Snowball"
(3 rows)
```

### 3) Handling missing paths

The following example attempts to find an element whose path does not exist:

```sql
SELECT jsonb_path_query_first(data, '$.email')
FROM person;
```

Output:

```text
 jsonb_path_query_first
------------------------
 null
 null
 null
(3 rows)
```

In this case, the person object doesn't have an `email` key, therefore the result is `NULL`.

## Summary

- Use the `jsonb_path_query_first()` function to extract the first JSON value that matches a JSON path expression from a JSON document.

---

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