JSONPath Tutorial: Query JSON Like a Pro
Modern APIs return deeply nested JSON — arrays of objects inside objects inside arrays. Extracting exactly what you need without writing nested loops is what JSONPath is for. Think of it as XPath for JSON: a concise query language that lets you drill into any level of a JSON document with a single expression.
What is JSONPath?
JSONPath was created by Stefan Goessner in 2007 and formally standardised in RFC 9535 (published February 2024). A JSONPath expression is a path that navigates from the root of a JSON document to the data you want.
The expression $.store.book[0].title means: from the root ($), go into store, then book, take the first element ([0]), and return its title.
The Sample JSON
All examples in this tutorial use this bookstore JSON (adapted from Goessner's original):
{
"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
}
}
}
Core JSONPath Syntax
| Symbol | Meaning |
|---|---|
$ | Root of the document |
.key | Child property (dot notation) |
['key'] | Child property (bracket notation — handles special chars) |
[n] | Array index (0-based) |
[*] | Wildcard — all elements of an array or all properties of an object |
.. | Recursive descent — search all levels |
[a,b] | Union — multiple indices or keys |
[start:end] | Array slice (Python-style) |
[?(@.key op value)] | Filter expression |
10+ Practical Examples
1. All book titles
$.store.book[*].title
// ["Sayings of the Century", "Sword of Honour", "Moby Dick", "The Lord of the Rings"]
2. All authors recursively
$..author
// ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"]
The .. operator searches at all depths — it would find author even nested 10 levels deep.
3. Everything in the store
$.store.*
// Returns: the book array and the bicycle object
4. Price of everything in the store
$.store..price
// [8.95, 12.99, 8.99, 22.99, 19.95] (all book prices + bicycle)
5. First book
$.store.book[0]
// {"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95}
6. Last book
$.store.book[-1]
// {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","price":22.99}
7. First two books (union and slice)
$.store.book[0,1]
// or:
$.store.book[0:2]
8. Books that have an ISBN
$.store.book[?(@.isbn)]
// Returns Moby Dick and The Lord of the Rings (only books with an isbn property)
9. Books under $10
$.store.book[?(@.price < 10)]
// Returns Sayings of the Century ($8.95) and Moby Dick ($8.99)
10. Fiction books under $15
$.store.book[?(@.category == "fiction" && @.price < 15)]
// Returns Sword of Honour ($12.99) and Moby Dick ($8.99)
11. All prices anywhere in the document
$..price
// [8.95, 12.99, 8.99, 22.99, 19.95]
12. Title of books costing exactly $8.95
$.store.book[?(@.price == 8.95)].title
// ["Sayings of the Century"]
Filter Expressions in Depth
Filter expressions use [?(@.property operator value)]. The @ is the current element being tested.
| Operator | Meaning | Example |
|---|---|---|
== | Equal | ?(@.category == "fiction") |
!= | Not equal | ?(@.category != "reference") |
< | Less than | ?(@.price < 10) |
<= | Less than or equal | ?(@.price <= 9) |
> | Greater than | ?(@.price > 20) |
>= | Greater than or equal | ?(@.price >= 12) |
| (no operator) | Property exists | ?(@.isbn) |
JSONPath in Code
JavaScript — using jsonpath-plus:
import { JSONPath } from 'jsonpath-plus'
const titles = JSONPath({
path: '$.store.book[?(@.price < 10)].title',
json: storeData
})
// ['Sayings of the Century', 'Moby Dick']
Python — using jsonpath-ng:
from jsonpath_ng import parse
expr = parse('$.store.book[?(@.price < 10)].title')
matches = [m.value for m in expr.find(data)]
# ['Sayings of the Century', 'Moby Dick']
Test JSONPath Queries Live
JSONPath vs JMESPath vs jq
| Feature | JSONPath | JMESPath | jq |
|---|---|---|---|
| Standard | RFC 9535 (2024) | RFC draft | None (de facto) |
| Used by | Kubernetes, Jayway, many tools | AWS CLI | Command line |
| Recursive search | Yes (..) | No | Yes |
| Filter expressions | Yes | Yes | Yes (very powerful) |
| Built-in functions | Limited | Rich (sort, join, etc.) | Extensive |
| Learning curve | Low–medium | Medium | High |
Frequently Asked Questions
Is JSONPath a standard?
JSONPath was originally defined informally by Stefan Goessner in a 2007 blog post. RFC 9535 (published February 2024) provides the first formal standardisation. Many libraries still implement the 2007 spec with their own extensions, so behaviour can differ between implementations.
What does @ mean in JSONPath?
The @ symbol refers to the current node being evaluated inside a filter expression. In $.store.book[?(@.price < 10)], the @ refers to each book object — @.price accesses the price property of that current element.
What is the difference between JSONPath and JMESPath?
JSONPath is more XPath-like with a recursive descent operator (..) and is widely used in Kubernetes and REST API testing frameworks. JMESPath is used by the AWS CLI and has richer built-in functions for projections and sorting. jq is a full command-line tool with a Turing-complete query language — very powerful but with a steeper learning curve.
Test JSONPath expressions live
IRJSON's Query tool supports JSONPath, JMESPath, and jq — test all three in your browser for free.
Open Query Tool →