JSONPath Definition
JSONPath is a declarative query language for JSON, standardized by IETF RFC 9535 (2024). It enables XPath-like navigation of JSON trees without writing imperative traversal code.
Core JSONPath Syntax
$— Root of the document..keyor["key"]— Access object property.[n]— Access array element at index n (0-based).[*]— Wildcard — all elements or properties...— Recursive descent — search all levels.[-1]— Last element in an array.[0,2]— Union — specific indices.[0:3]— Slice — elements from index 0 to 2.[?(@.price < 10)]— Filter expression — elements matching condition.
// Sample JSON:
{
"store": {
"books": [
{ "title": "Moby Dick", "price": 8.99, "category": "classic" },
{ "title": "Dune", "price": 12.99, "category": "scifi" },
{ "title": "Hamlet", "price": 6.99, "category": "classic" }
]
}
}
// JSONPath Queries:
$.store.books[*].title // All book titles
$.store.books[0].price // Price of first book: 8.99
$.store.books[-1] // Last book (Hamlet)
$..price // All prices anywhere: [8.99, 12.99, 6.99]
$.store.books[?(@.price < 10)] // Books under $10
$.store.books[?(@.category == "classic")].title // Classic book titles