JSONPath Tutorial: Syntax and Examples
JSONPath is a small query language for JSON, similar to what XPath is for XML. With one short expression you can pull every price out of a large document, find items that match a condition, or point to one exact value. It is used in API testing tools, monitoring systems, Kubernetes (kubectl -o jsonpath) and many data pipelines. In 2024 it was standardized as RFC 9535.
The example document
All examples below use this JSON. You can paste it into the JSONPath Tester and try each expression yourself.
{
"store": {
"book": [
{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
{ "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
{ "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
{ "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}1. The root: $
Every expression starts with $, which means "the whole document". On its own, $ returns everything.
2. Children: .name and ['name']
$.store.bicycle.color โ "red"
$['store']['bicycle']['color'] โ "red"Both forms mean the same thing. Use the bracket form when a key contains spaces, dashes or dots: $['first name'].
3. Array items: [0], [-1]
$.store.book[0].title โ "Sayings of the Century"
$.store.book[-1].title โ "The Lord of the Rings" (last item)
$.store.book[0,2].title โ "Sayings of the Century", "Moby Dick"Indexes start at 0. Negative indexes count from the end.
4. Slices: [start:end:step]
$.store.book[0:2] โ first two books (index 0 and 1)
$.store.book[1:] โ every book except the first
$.store.book[::2] โ every second book (index 0, 2)Like in Python, the end index is not included.
5. Wildcard: *
$.store.book[*].author โ all four authors
$.store.* โ the book array and the bicycle object6. Recursive descent: ..
.. searches at every depth. It is the most useful operator when you don't know (or don't care) exactly where something is:
$..author โ all authors, wherever they are
$..price โ 8.95, 12.99, 8.99, 22.99, 19.95 (books and the bicycle)$..something has to visit every value, so it is slower than a direct path like $.store.book[*].price.7. Filters: [?( )]
Filters keep only the items that match a condition. Inside a filter, @ means "the current item".
$.store.book[?(@.price < 10)].title
โ "Sayings of the Century", "Moby Dick"
$.store.book[?(@.isbn)].title
โ books that have an isbn: "Moby Dick", "The Lord of the Rings"
$..book[?(@.category == 'fiction' && @.price > 10)].title
โ "Sword of Honour", "The Lord of the Rings"
$..book[?(@.author =~ /tolkien/i)].title
โ "The Lord of the Rings" (regular expression, i = ignore case)Comparison operators: ==, !=, <, <=, >, >=. Combine conditions with && (and), || (or) and ! (not). In RFC 9535 the parentheses are optional, so [?@.price < 10] also works.
Cheat sheet
| Expression | Meaning |
|---|---|
$ | Root of the document |
.key or ['key'] | Child by name |
[0] [-1] | Array item by index |
[0,2] | Several items |
[1:3] [::2] | Slice: start, end, step |
* | All children |
..key | Key at any depth |
[?(@.x > 1)] | Filter items |
Differences between tools
Before RFC 9535, JSONPath was defined only by a 2007 blog post, so libraries differ in details: regular-expression syntax, function extensions, and how results are ordered. Some tools add extras, such as .length for the size of an array, which the JSONPath Tester also supports. If an expression works in one tool and not another, try the plainest form first.
A practical workflow
- Open a big file in the JSON Viewer, click the value you need and press Copy path, for example
$.store.book[2].price. - Generalize it in the JSONPath Tester: replace
[2]with[*]to get every price, or add a filter. - Use the final expression in your test tool, monitoring rule or code.