JSONPath is a query language for locating and extracting data from JSON documents, inspired by XPath. When you face deeply nested API responses or config files, you don't need nested loops — one expression can return the fields you need.
Basic syntax
JSONPath uses $ for the root, dot notation for object properties, and brackets for array indexes or keys. Common patterns:
- $.store.book[0].title — first book title
- $.store.book[*].author — all authors
- $..price — all price fields recursively
- $.store.book[?(@.price < 10)] — books priced under 10
Typical use cases
- Debug API responses: confirm nested fields exist and values are correct
- Log analysis: pull traceId, error codes from structured JSON logs
- Automated tests: assert specific paths in JSON responses
- Config extraction: slice a subtree from a large config document
Sample data
{
"store": {
"book": [
{ "title": "Sayings of the Century", "price": 8.95 },
{ "title": "Moby Dick", "price": 8.99 }
]
}
}For the sample above, $.store.book[*].title returns both titles. Paste your JSON and expression into the JSONPath tester first, then copy the working query into code or tests.