JSONPath is a small query language for pulling values out of a JSON document without writing loops. One expression — $.user.roles[0] — replaces a chain of manual lookups, and it shines when you need every matching value at any depth. This is a practical cheat sheet: each operator comes with a copy-paste example you can run live in the JSONPath tester.
JSONPath was standardised as RFC 9535 in 2024, which finally pinned down behaviour that used to vary between libraries. New to JSON structure? See what JSON is first.
A sample document
Every example below runs against this JSON:
{
"store": {
"book": [
{ "title": "JSON Basics", "price": 9 },
{ "title": "Advanced APIs", "price": 24 },
{ "title": "Regex in Depth", "price": 15 }
],
"bicycle": { "color": "red", "price": 199 }
}
}
The core operators
| Expression | Selects |
|---|---|
$ | The root of the document. |
$.store.bicycle.color | A direct child by name → "red". |
$.store.book[0] | The first element of an array (zero-based). |
$.store.book[-1] | The last element. |
$.store.book[*].title | Every title — * is the wildcard. |
$..price | Every price at any depth — .. is recursive descent. |
$.store.book[0,2] | A union — the first and third books. |
$.store.book[:2] | An array slice — the first two books. |
Filter expressions
Filters are where JSONPath earns its keep. Inside [?()], @ refers to the current element, so you can keep only the items that match a condition:
$.store.book[?(@.price < 10)] # books under 10 -> "JSON Basics"
$.store.book[?(@.price >= 15)] # 15 or more -> two books
$.store.book[?(@.title == "JSON Basics")] # match by value
Combine conditions with && and ||, and compare against numbers, strings or booleans. Build and verify your filter against real data in the tester before you put it in code — a misplaced quote is the usual cause of “no results”.
Wildcards and recursive descent together
The real power is combining operators. To collect every price in the store regardless of nesting, use recursive descent; to collect only book titles, use a wildcard over the array:
$..price # 9, 24, 15, 199 (books and the bicycle)
$.store..title # every title under store
$..book[?(@.price < 20)].title # titles of the cheaper books
If you do not know the exact shape of a payload, the JSON path finder shows the path to any value you click, which you can copy straight into your query.
Using JSONPath in code
Libraries exist for every language — jsonpath-plus in JavaScript, jsonpath-ng in Python, Jayway in Java. The expression is identical; only the function call differs:
// JavaScript (jsonpath-plus)
import { JSONPath } from "jsonpath-plus";
const prices = JSONPath({ path: "$..price", json: data });
In Python you would parse the payload first (see parsing JSON in Python) and then apply the expression with jsonpath-ng. Because the expression is just a string, you can keep your queries in config and change what you extract without touching code — a common pattern in data-pipeline and scraping tools, where the shape of the source changes more often than the logic around it.
Flattening vs querying
JSONPath pulls specific values out of a structure. If instead you want the whole document turned into flat, dot-notation keys — useful for spreadsheets or search indexing — reach for the JSON flatten tool. Querying and flattening solve different problems: query when you want a few values, flatten when you want every leaf.
A real-world example: trimming an API response
Say an endpoint returns a large order with dozens of fields, but your UI only needs each line item’s SKU and quantity. Rather than loop and reshape by hand, one expression collects exactly what you need: $.order.items[*].sku for the codes, or a filter like $.order.items[?(@.quantity > 0)] to skip empty lines. Pointing JSONPath at the parsed response keeps the extraction declarative — the query reads like the shape of the data — and isolates you from the fields you do not care about, so an unrelated change to the payload does not break your code. Prototype the expression against a captured sample in the tester, confirm it returns the rows you expect, then move it into the application.
Array slices and unions in detail
Beyond single indexes, JSONPath borrows Python-style slicing for arrays. The syntax is [start:end:step], where end is exclusive and any part can be omitted:
$.store.book[:2] # first two books (index 0 and 1)
$.store.book[1:] # everything from the second book on
$.store.book[::2] # every other book
$.store.book[-2:] # the last two books
A union [0,2] picks specific, non-contiguous indexes, and the same bracket can union named fields in some implementations. Slices are the clean way to page through or sample a large array without a loop.
JSONPath vs jq and JMESPath
JSONPath is not the only JSON query language. jq is a command-line powerhouse with its own syntax, transformations and the ability to reshape output — overkill for a simple lookup but unbeatable in a shell pipeline. JMESPath (used by the AWS CLI) is another expression language with strong projection support. JSONPath's advantage is familiarity and ubiquity: the syntax reads like the object path you would write by hand, and libraries exist everywhere. For pulling a few values out of an API response in application code, JSONPath is usually the lightest choice.
Common pitfalls
- Quoting in filters. String comparisons need quotes:
[?(@.name == "Ada")], not== Ada. - Result is always a list. Even a single match comes back wrapped in an array — take the first element if you expect one value.
- Missing paths return empty, not an error. A typo in a key yields no results rather than throwing, so verify the expression against real data in the tester.
- Recursive descent can be expensive on huge documents because it visits every node — prefer a direct path when you know the shape.
Key takeaways
$is the root;.is a direct child and..is recursive descent.[*]is the wildcard;[n],[-1],[a,b]and[:n]index, union and slice arrays.- Filters
[?(@.field op value)]keep only matching elements. - Prototype expressions in the JSONPath tester, then drop them into any library.