liveBook · Manning

Json path expression

As explained in Just Use Postgres!

SQL/JSON path language is a standardized language for navigating and filtering JSON data within SQL expressions.

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.

How Just Use Postgres! covers it

How deep it goes
Builds paths from $, accessors, arrays, and filters, while deferring a complete treatment of every operator.
Examples
Demonstrates pizza-type counts, cheese-array queries, sausage filters, and chained conditions on custom pizzas.
What it assumes
Assumes basic PostgreSQL JSON querying and introduces path symbols and operators progressively from simpler use cases.
What you can do after
Apply it via PostgreSQL JSONB functions to extract fields, inspect arrays, and filter multilevel JSON structures.
How it connects
Connects JSON path expressions with PostgreSQL JSONB, arrays, filters, subqueries, and JSON operators.

Also explained in

Compare how 2 books explain json path expression →

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.