What JSONPath does
JSONPath is a query language for selecting values inside a JSON document, in the same way that XPath selects nodes in XML. Instead of writing loops and null checks in code, you describe the path to the data you want and the engine returns every matching value. Stefan Goessner proposed the syntax in 2007, and after many slightly different implementations it was standardized as RFC 9535 in 2024.
JSONPath is used in API testing tools, Kubernetes kubectl output formatting, AWS Step Functions, log processors, and many integration platforms. Testing an expression against real sample data before putting it into one of those systems saves a lot of trial and error.
Core syntax
Every expression starts with $, which represents the root of the document. Child members are selected with dot notation such as $.store.name, or with bracket notation such as $['store']['name']. Bracket notation is required when a key contains spaces, hyphens, or other special characters.
Arrays are indexed from zero: $.items[0] is the first element and $.items[-1] is the last. Slices work like Python, so $.items[0:3] returns the first three elements and $.items[::2] returns every second element. The wildcard * selects all members of an object or all elements of an array, so $.items[*].id returns the id of every item.
The double dot .. performs a recursive descent, searching every level of the document. $..price finds every price field no matter how deeply it is nested. It is powerful but can return more than you expect on large documents, so combine it with a more specific path when you can.
Filtering with expressions
Filter expressions select array elements that meet a condition. They are written as [?(...)] in classic syntax or [?...] in RFC 9535, and the @ symbol refers to the current element. For example, $.books[?(@.price < 10)].title returns the titles of books cheaper than 10, and $.users[?(@.active == true)] returns only active users.
Filters can compare against strings and numbers, check whether a member exists with an expression such as [?(@.email)], and combine conditions with && and ||. Because implementations historically differed in how they handle filters, regular expressions, and functions, always test an expression with the same flavor your production tool uses.
Practical tips
Build expressions incrementally. Start with $ and add one segment at a time, checking the result after each step. When a query returns nothing, the most common causes are a typo in a key name, the wrong letter case, or treating an object as an array.
Remember that JSONPath always returns a list of matches, even when only one value is found, and an empty list when nothing matches. Code that consumes the result should handle both cases. For simple direct lookups, a JSON Pointer such as /store/books/0/title is an alternative that addresses exactly one location, and it is the format used by JSON Schema error messages and JSON Patch operations.
Common JSONPath patterns
A few expressions cover most everyday needs. $.data[*].id collects one field from every element of an array. $..email finds every email address at any depth, which is handy when exploring an unfamiliar API response. $.orders[?(@.status == 'failed')].id returns the IDs of failed orders, and $.items[-1:] returns the last element of a list as a one-item result.
When a document has an unpredictable structure, combine recursive descent with a filter, for example $..[?(@.type == 'error')], to find every object of a certain kind wherever it appears. Keep such broad queries for exploration, and switch to explicit paths in production configuration so that unrelated changes elsewhere in the document cannot change the result.