Working with JSON is a daily reality for most developers, but pulling specific values out of large, deeply nested structures can quickly become tedious. That's where JSONPath comes in. Below is a hands-on guide to what it is and how to use it effectively.
What Is JSONPath?
JSONPath is a query language for JSON, built in the same spirit as XPath is for XML. It gives you a compact way to reach into complex, nested JSON documents and extract exactly the values you need using simple path expressions—no manual loops or nested key lookups required.
Root & Navigation Basics
Every JSONPath expression starts at the root and drills down from there. These are the core building blocks:
| Expression | Description | Example |
|---|---|---|
$ | Root of the document | $ |
. | Child operator | $.name |
.. | Recursive descent (deep scan) | $..price |
* | Wildcard — matches everything | $.store.* |
[n] | Array index | $.items[0] |
[-n] | Nth element from the end | $.items[-1] |
[start:end] | Array slice | $.items[0:3] |
[a,b] | Union — select multiple indices | $.items[0,2] |
Filter Expressions
Filters let you select nodes conditionally rather than by position. They use the ?() syntax, and inside a filter the @ symbol refers to the current node being evaluated.
| Expression | What It Does |
|---|---|
$.books[?(@.price < 10)] | Books priced under 10 |
$.books[?(@.author == "Tolkien")] | Books by a specific author |
$.books[?(@.isbn)] | Books that have an ISBN field |
$.books[?(@.price > 5 && @.price < 20)] | Books within a price range |
$.users[?(@.role == "admin")] | Users with the admin role |
Common Real-World Examples
Here are some patterns you'll reach for again and again:
Get all prices from a store
$.store.book[*].price
Get the title of the first book
$.store.book[0].title
Get every author anywhere in the document
$..author
Get books cheaper than $10
$.store.book[?(@.price < 10)]
Get the last item in an array
$.items[-1]
Get the name and email from all users
$.users[*]['name','email']
Operators Reference
When building filter expressions, these are the operators you have to work with:
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
&& | Logical AND |
|| | Logical OR |
=~ | Regex match (implementation-dependent) |
JSONPath vs. JMESPath — Quick Comparison
JSONPath isn't the only JSON query language out there. JMESPath is a popular alternative, especially in the AWS ecosystem. Here's how they stack up:
| Feature | JSONPath | JMESPath |
|---|---|---|
| Standard | Informal (RFC draft) | Formal specification |
| Syntax | $.store.book[*] | store.book[*] |
| Filtering | ?(@.price < 10) | books[?price < \10`]` |
| Commonly used in | Most JSON tools | AWS CLI, jq-style tools |
| Browser support | Wide | Wide |
Summary
JSONPath shines when you need to extract data from JSON quickly and expressively, whether you're filtering by field values, scanning deeply nested structures, or grabbing specific array elements. Keep this reference handy, and those tangled JSON payloads will feel a lot more manageable.
Note: JSONPath implementations can differ slightly between libraries and tools—particularly around filters and regex support—so it's always worth checking the docs for whichever engine you're using.