JSONPath Cheat Sheet: Syntax and Examples
JSONPath is a query language for JSON, similar to XPath for XML. It lets you extract values from JSON structures using path expressions. This reference covers all the syntax you need.
Sample JSON used in examples
{
"store": {
"name": "Books & More",
"books": [
{ "title": "Clean Code", "author": "Robert Martin", "price": 35.99, "inStock": true },
{ "title": "The Pragmatic Programmer", "author": "Hunt & Thomas", "price": 45.00, "inStock": true },
{ "title": "Design Patterns", "author": "Gang of Four", "price": 54.95, "inStock": false },
{ "title": "Refactoring", "author": "Martin Fowler", "price": 42.50, "inStock": true }
],
"location": { "city": "San Francisco", "zip": "94105" }
}
}Syntax quick reference
| Syntax | Meaning |
|---|---|
$ | Root element |
.key | Child key (dot notation) |
['key'] | Child key (bracket notation) |
.. | Recursive descent — search all levels |
[*] | Wildcard — all elements of an array |
[0] | Array index (zero-based) |
[-1] | Last element |
[0,2] | Multiple indexes |
[0:3] | Slice — elements 0, 1, 2 |
[?(@.key)] | Filter — elements where key exists |
[?(@.key == val)] | Filter — elements where key equals val |
[?(@.key > val)] | Filter — elements where key > val |
Examples
Basic property access
$.store.name
→ "Books & More"
$.store.location.city
→ "San Francisco"
$.store.location['zip']
→ "94105"Array access
$.store.books[0].title
→ "Clean Code"
$.store.books[-1].title
→ "Refactoring"
$.store.books[1].price
→ 45.00Wildcard — all elements
$.store.books[*].title
→ ["Clean Code", "The Pragmatic Programmer", "Design Patterns", "Refactoring"]
$.store.books[*].price
→ [35.99, 45.00, 54.95, 42.50]Array slices
$.store.books[0:2].title
→ ["Clean Code", "The Pragmatic Programmer"]
$.store.books[2:].title
→ ["Design Patterns", "Refactoring"]Multiple indexes
$.store.books[0,3].title
→ ["Clean Code", "Refactoring"]Recursive descent
$..title
→ ["Clean Code", "The Pragmatic Programmer", "Design Patterns", "Refactoring"]
$..price
→ [35.99, 45.00, 54.95, 42.50]Filter expressions
// Books that are in stock
$.store.books[?(@.inStock == true)].title
→ ["Clean Code", "The Pragmatic Programmer", "Refactoring"]
// Books cheaper than $40
$.store.books[?(@.price < 40)].title
→ ["Clean Code"]
// Books $40 or more
$.store.books[?(@.price >= 40)].title
→ ["The Pragmatic Programmer", "Design Patterns", "Refactoring"]Combining conditions
// In-stock books under $50
$.store.books[?(@.inStock == true && @.price < 50)].title
→ ["Clean Code", "The Pragmatic Programmer", "Refactoring"]Implementation differences
JSONPath was originally described by Stefan Goessner in 2007 with no formal standard. Different libraries implement it differently. Common variation points:
- Negative indexes —
[-1]works in most implementations but not all. - Regex filters —
=~operator is not universally supported. - RFC 9535 — A formal JSONPath standard was published in 2024. New libraries may behave differently from older ones.
JSONPath vs jq
jq is a command-line JSON processor with its own more powerful query language. Use JSONPath when embedding queries in application code or config files. Use jq for shell scripting and one-off transformations in the terminal.
Test JSONPath expressions live
Paste any JSON and write JSONPath expressions in Jsonic's JSONPath Tester. Results update as you type. Nothing is uploaded.
Open JSONPath Tester