liveBook · Manning

Jsonb_path_query

As explained in Just Use Postgres!

jsonb_path_query is a PostgreSQL function that evaluates a JSON path against a jsonb value and returns the matching JSON items.

jsonb_path_query evaluates a JSON path against a jsonb value and returns the matching JSON items. A path starts at $ and navigates fields with .key. section 5.4.4

Group pizzas by type

SELECT count(*) AS total_cnt, jsonb_path_query(pizza, '$.type') AS pizza_type FROM pizzeria.order_items GROUP BY pizza_type ORDER BY total_cnt DESC; The query extracts each pizza's type, groups matching values, and produces counts such as pepperoni, three cheese, veggie, custom, and margherita. section 5.4.4

Use JSON path expressions to navigate objects and arrays:

  • $.toppings.cheese[*] navigates to the cheese array and returns every object in it. section 5.4.4
  • Pass returned objects to jsonb_object_keys to extract cheese names such as mozzarella, cheddar, and parmesan. section 5.4.4
  • Combine extraction with grouping and counting to build business reports from JSON data. section 5.4.4
Listing 5.7 Accessing JSON fields with path expressions
SELECT 
    count(*) as total_cnt, 
    jsonb_path_query(pizza,'$.type') as pizza_type
FROM pizzeria.order_items
GROUP BY pizza_type ORDER BY total_cnt DESC;

Runnable SQL demonstrates extracting pizza types with jsonb_path_query and aggregating the results. section 5.4.4

How Just Use Postgres! covers it

How deep it goes
Explains object access, array wildcards, and aggregation examples, while deferring broader JSON path operators.
Examples
Uses pizza types, cheese arrays, jsonb_object_keys, and ranked report outputs as concrete query examples.
What it assumes
Assumes JSON data and SQL aggregation context while introducing JSON path navigation from simpler use cases.
What you can do after
Apply it via PostgreSQL SQL to extract JSON values and aggregate matching items in reports.
How it connects
Connects JSON path expressions with JSON arrays, jsonb_object_keys, grouping, and counting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.