JSONPath Explained: Query JSON with Examples
When you need one value out of a large JSON document, writing code to walk the nested objects and arrays is overkill. JSONPath is a compact query language built for exactly this — the JSON equivalent of XPath for XML. You write a short expression and get back every matching value. This guide breaks the syntax down piece by piece, with examples you can run against your own data.
What is JSONPath?
A JSONPath expression describes a route through a document. It always starts at the root, written as $, and then steps into the data using dot or bracket notation until it lands on the value — or values — you're after. Where a path could match more than one place, JSONPath returns all of them as a list.
The syntax, piece by piece
These building blocks cover almost everything you'll write:
- $ — the root of the document; every expression begins here.
- .key or ['key'] — step into a named property.
- [0], [-1] — an array element by index, counting from the end with negatives.
- [*] or .* — every element of an array or every value of an object.
- .. — recursive descent: search every level of the document.
- [start:end] — an array slice, e.g. [0:3] for the first three items.
- [?(@.field == value)] — a filter that keeps only matching elements.
Common queries by example
Put together, the pieces read naturally:
- $.store.book[*].author — the author of every book.
- $..price — every price anywhere in the document, at any depth.
- $.items[0] — the first item in the items array.
- $.users[?(@.active)] — only the users whose active flag is truthy.
- $.products[?(@.price < 10)] — products cheaper than 10.
Recursive descent and filters
The two most powerful operators are recursive descent (..) and filters. Recursive descent ignores structure entirely — $..id finds every id no matter how deeply it's nested, which is perfect for irregular documents where you don't know the exact path.
Filters narrow a set of elements by a condition written against the current item, referenced as @. Inside a filter you can compare fields (@.price < 10), test for presence (@.discount), and combine conditions, so you can select precisely the elements you care about.
Where JSONPath is used
JSONPath shows up across the tooling developers use every day: extracting fields in API gateways and integration platforms, the JSONPath output format in kubectl, assertions in API test suites, and log and event pipelines. Learning it once pays off in all of them — and testing an expression live before you commit it to a config saves a lot of trial and error.
Try it yourself
Write a JSONPath expression and see exactly which values it matches.