JSONPath is a query language for JSON, similar to XPath for XML. It lets you extract specific values from deeply nested JSON structures using path expressions. Used heavily in APIs, testing frameworks, and configuration tools.
{
"store": {
"books": [
{ "title": "Clean Code", "author": "Robert Martin", "price": 34.99, "category": "tech" },
{ "title": "The Pragmatic Programmer", "author": "Hunt & Thomas", "price": 49.99, "category": "tech" },
{ "title": "Sapiens", "author": "Yuval Harari", "price": 18.99, "category": "history" },
{ "title": "Atomic Habits", "author": "James Clear", "price": 16.99, "category": "self-help" }
],
"location": "New York"
}
}| Operator | Description |
|---|---|
| $ | Root node — every expression starts here |
| .key | Child key selector |
| ..key | Recursive descent — searches all levels |
| [*] | Wildcard — selects all elements of an array or object |
| [n] | Array index. Negative index counts from end. |
| [a:b] | Array slice from index a to b (exclusive) |
| [a,b] | Multiple indices — union selector |
| ?(expr) | Filter expression — select items where expr is true |
| @ | Current node (used inside filter expressions) |
| Expression | What it does | Result |
|---|---|---|
| $ | Root element | The entire document |
| $.store | Child element | The store object |
| $.store.books | Nested child | Array of all books |
| $.store.books[0] | Array index | First book object |
| $.store.books[-1] | Last item | Last book object |
| $.store.books[*] | Wildcard — all items | All book objects |
| $.store.books[*].title | All titles | ["Clean Code", "The Pragmatic Programmer", ...] |
| $.store.books[0,2] | Multiple indices | First and third books |
| $.store.books[0:2] | Slice (0 to 2) | First two books |
| $..title | Recursive descent — all titles anywhere | All title values in the document |
| $..price | All price values | All price numbers |
| $.store.books[?(@.price < 20)] | Filter: price under 20 | Sapiens and Atomic Habits |
| $.store.books[?(@.category == "tech")] | Filter: by category | Clean Code and Pragmatic Programmer |
| $.store.books[?(@.author =~ /James/)] | Filter: regex match | Atomic Habits |
| $.store.books.length | Array length | 4 |
Filter expressions use ?(@.field operator value) syntax. @ refers to the current item.