A JSON path expression navigates nested JSON objects and arrays inside PostgreSQL queries. It can retrieve values, test whether matching data exists, and replace longer subqueries with compact, readable path rules. section 5.4.4
Build a path from the JSON value outward:
- Start with $ for the JSON object being queried. section 5.4.4
- Follow object fields with accessors such as .type or .toppings.cheese. section 5.4.4
Find pizzas with sausage toppings
SELECT order_id, order_item_id, pizza->'toppings'->'meats' AS meats FROM pizzeria.order_items WHERE jsonb_path_exists( pizza, '$.toppings.meats[*] ? (exists(@.sausage))' ) ORDER BY order_id LIMIT 5; The same style of expression can filter custom pizzas whose parmesan topping has an extra quantity; that query returns 64 rows.

