JSONPath Cheat Sheet
Quick reference for JSONPath expressions: root, child, wildcard, filters, slices, and JSONPath-Plus extensions.
JSONPath is a query language for extracting data from JSON documents, inspired by XPath for XML. The original draft (Goessner) defines the core syntax; the JSONPath-Plus community spec adds filters, functions, and operators. Implementations differ slightly, so test expressions against your target parser.
Core Syntax
| Expression | Meaning | Example |
|---|---|---|
$ | The root object | $.name |
.name | Child access | $.store.book |
['name'] | Bracket child access (needed for keys with special chars) | $['store']['book'] |
.* | All direct children | $.store.* |
.. | Recursive descent — all descendants at any depth | $..author |
[0] | Array index | $.book[0] |
[-1] | Last element | $.book[-1] |
[0,2] | Multiple indices | $.book[0,2] |
[0:3] | Slice — indices 0, 1, 2 | $.book[0:3] |
[::2] | Slice with step | $.book[::2] |
[?()] | Filter expression | $.book[?(@.price < 10)] |
['*'] | All keys of an object | $.*['*'] |
Example Document
{
"store": {
"book": [
{ "category": "fiction", "title": "Book A", "price": 8.95 },
{ "category": "fiction", "title": "Book B", "price": 12.99 },
{ "category": "reference", "title": "Book C", "price": 8.99 }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}
Queries on the Example
| JSONPath | Result |
|---|---|
$.store.book[0].title | "Book A" |
$..title | All book titles at any depth |
$.store.* | The book array and the bicycle object |
$.store.book[?(@.price < 9)] | Books A and C |
$.store.book[?(@.category == 'fiction')].title | Titles of fiction books |
$..book[1,2] | Books B and C |
$.store.book[-1:] | The last book |
Filter Expressions
Filters use ?() with an expression evaluated per item. The placeholder @ is the current item; $ can reach back to the root.
| Operator | Meaning | Example |
|---|---|---|
== | Equality (with type coercion in most impls) | [?(@.category == 'fiction')] |
!= | Inequality | [?(@.category != 'reference')] |
<, <=, >, >= | Numeric comparison | [?(@.price >= 10)] |
&& / || | Logical and / or | [?(@.price < 10 && @.category == 'fiction')] |
! | Negation | [?(!@.isDiscounted)] |
=~ | Regex match | [?(@.title =~ /Book [AB]/)] |
in | Value in array | [?(@.category in ['fiction'])] |
nin | Value not in array | [?(@.category nin ['reference'])] |
size | Array/string length (JSONPath-Plus) | [?(@.title size >= 6)] |
@.key | Child access inside filter | [?(@.store.bicycle.color == 'red')] |
Slices in Detail
Slices follow [start:end:step] semantics from Python. Negative indices count from the end.
| Slice | Meaning |
|---|---|
[1:3] | Indices 1 and 2 |
[:2] | Indices 0 and 1 |
[2:] | From index 2 to the end |
[-2:] | The last two elements |
[::-1] | The whole array reversed |
[::2] | Every second element |
Functions & Length (JSONPath-Plus)
JSONPath-Plus adds length(), count(), keys(), value(), first(), last(), sort(), reverse(), sum() and string helpers.
| Expression | Meaning |
|---|---|
$.store.book.length() | Number of books |
$.store.book.sum(@.price) | Total price of books |
$.store.book[?(@.category == 'fiction')].length() | Count of fiction books |
$..keys() | All property names anywhere |
Common Pitfalls
[!WARNING]
==with numbers vs strings can behave differently across implementations; some parse'8.95'(string) differently from8.95(number). Quote values consistently.
[!TIP] Use
[0:1](a slice) instead of[0]when you need the result to stay an array — some tools collapse single-value results.