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

# PostgreSQL jsonb_path_query() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `jsonb_path_query()` function to query `JSONB` data using JSON path expressions.

## Introduction to the PostgreSQL jsonb_path_query() function

The `jsonb_path_query()` function allows you to query [JSONB](../postgresql-tutorial/postgresql-json) data based on a JSON path expression.

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

```sql
jsonb_path_query(jsonb_data, path_expression)
```

In this syntax:

- `jsonb_data` is the JSONB data that you want to query.
- `path_expression` is a JSON path expression that locates values or elements in the JSONB data.

The `jsonb_path_query()` function returns JSONB data that matches the specified JSON path expression.

If the `path_expression` does not locate any element in the `jsonb_data`, the function returns `NULL`.

## PostgreSQL jsonb_path_query() function example

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

### Setting up a sample table

First, [create a table](../postgresql-tutorial/postgresql-create-table) named `products`with a JSONB column names `attributes` to store product attributes:

```sql
CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    attributes JSONB
);
```

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

```sql
INSERT INTO products (name, attributes)
VALUES
    ('Laptop', '{"brand": "Dell", "price": 1200, "specs": {"cpu": "Intel i7", "ram": "16GB"}}'),
    ('Smartphone', '{"brand": "Samsung", "price": 800, "specs": {"os": "Android", "storage": "128GB"}}');
```

### 1) A basic jsonb_path_query() function example

The following example uses the `jsonb_path_query()` function to retrieve the brand and price of all products:

```sql
SELECT jsonb_path_query(attributes, '$.brand') AS brand,
       jsonb_path_query(attributes, '$.price') AS price
FROM products;
```

Output:

```text
   brand   | price
-----------+-------
 "Dell"    | 1200
 "Samsung" | 800
(2 rows)
```

### 2) More complex JSON path example

The following example uses the `jsonb_path_query()` function to query nested attributes such as retrieving the CPU specification of laptops:

```sql
SELECT jsonb_path_query(attributes, '$.specs.cpu') AS cpu
FROM products;
```

Output:

```text
    cpu
------------
 "Intel i7"
(1 row)
```

## Summary

- Use the `jsonb_path_query()` function to query JSONB data based on JSON path expressions.

---

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