JSONPath selects values from nested JSON with expressions that begin at the root. It is useful in tests, monitoring, transformation tools and automation, but concise expressions can hide broad matches. Test both returned values and their locations before relying on a query.

Root, children and array indexes

The dollar sign represents the root value. Dot notation selects an object member with a simple name, while bracket notation handles spaces and punctuation. A numeric bracket selects one array position.

For a response shaped as store.books, $.store.books[0].title selects the title of the first book. The zero-based index is explicit and returns nothing when the array is empty, so production code should handle an absent match.

$.store.books[0].title
$['store']['books'][0]['title']

Wildcards and recursive descent

A wildcard selects every member or array item at one level. $.store.books[*].title returns each book title. Recursive descent searches through descendants and is convenient for discovery, but it can match fields from unrelated parts of a large document.

Prefer a specific path when the payload contract is known. Recursive expressions such as $..id may silently begin returning new IDs after an API adds another nested resource.

Slices and filters

Array slices select a positional range, while filters select items whose data meets a condition. A filter like $.store.books[?(@.price < 30)].title returns titles for books below a price threshold.

Filter capabilities and extension syntax historically differed among libraries. RFC 9535 standardized core JSONPath behavior, but implementations may still expose non-standard functions. Test expressions with the same library and version used by the destination system.

Treat zero, one and many as distinct outcomes

A JSONPath query naturally returns a sequence of matches. Even when you expect one value, decide what the application should do with zero or multiple matches. Taking the first result without checking can hide a contract change.

When debugging, display normalized paths alongside values. Two equal values from different locations look identical in a value-only list, while their paths reveal why the query returned more than expected.

Practical takeaways

  • Begin at $ and make paths as specific as practical.
  • Wildcards and recursion can return more than expected.
  • Confirm library support for filters and extensions.
  • Handle zero, one and many matches explicitly.

How this guide was prepared

JSON Anvil guides are written for working developers, checked against reproducible examples, and reviewed for technical clarity. Tool output is tested locally; readers should still validate behavior in the exact runtime used by their application.