JSONPath: How to Query and Filter JSON Data
JSONPath is a query language for JSON, providing the same kind of document-navigation power that XPath gives XML developers. Instead of writing nested loops or chained property accesses, you write a compact expression that pinpoints exactly the data you need — whether it lives three levels deep or is scattered across a hundred-element array.
What Is JSONPath and Why Does It Matter?
Stefan Goessner introduced JSONPath in 2007 as a direct analog to XPath 1.0 for XML. Just as XPath lets you write //book[@price<10] to pluck cheap books out of an XML document tree, JSONPath lets you write $.store.book[?(@.price < 10)] to do the same against a JSON structure. The syntax is deliberately terse and readable, making it a natural fit for configuration-driven data extraction.
The practical value of JSONPath is highest when you are working with API responses you do not fully control. Rather than parsing the entire payload and walking the object graph yourself, you hand a JSONPath expression to a library and get back exactly the nodes you care about.
The Root Operator and Basic Navigation
Every JSONPath expression starts with $, which represents the root of the JSON document. From there you navigate using either dot notation or bracket notation.
Dot notation chains property names with dots: $.store.book.title
Bracket notation wraps property names in single quotes inside square brackets: $['store']['book']['title']
Consider this sample document:
{
"store": {
"book": [
{ "title": "Sayings of the Century", "author": "Nigel Rees", "price": 8.95 },
{ "title": "Sword of Honour", "author": "Evelyn Waugh", "price": 12.99 },
{ "title": "Moby Dick", "author": "Herman Melville","price": 8.99 },
{ "title": "The Lord of the Rings", "author": "J.R.R. Tolkien","price": 22.99 }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}
Wildcards and Recursive Descent
The wildcard operator * matches all properties of an object or all elements of an array:
$.store.book[*].author
Returns every author value: ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "J.R.R. Tolkien"]
The recursive descent operator .. searches at any depth:
$..author
Finds every author field anywhere in the entire document tree, regardless of nesting depth.
Array Slicing and Index Access
$.store.book[0] /* first book */
$.store.book[-1] /* last book (negative index) */
$.store.book[0,2] /* first and third books (union) */
$.store.book[0:2] /* first two books (slice, exclusive end) */
$.store.book[1:3] /* second and third books */
$.store.book[*] /* all books */
Filter Expressions
Filter expressions let you select array elements that satisfy a condition. The syntax is [?( expression )] where @ refers to the current element:
/* books costing less than $10 */
$.store.book[?(@.price < 10)]
/* books that have an isbn property */
$.store.book[?(@.isbn)]
/* books by a specific author */
$.store.book[?(@.author == "Moby Dick")]
/* books priced between $8 and $13 */
$.store.book[?(@.price >= 8 && @.price <= 13)]
Built-in Functions
/* Number of books in the array */
$.store.book.length()
/* All keys of the store object */
$.store.keys()
/* Minimum price across all books */
$.store.book[*].price.min()
/* String matching */
$.users[?( match(@.name, "A.*") )]
Practical Examples
Extract all email addresses from a user list:
$.users[*].email
/* Result: ["alice@example.com", "bob@example.com", "carol@example.com"] */
Find all books under $15:
$.store.book[?(@.price < 15)]
/* Returns the three objects with prices 8.95, 12.99, and 8.99 */
Get the last element of an array:
$.store.book[-1]
/* Returns the Lord of the Rings object */
Where JSONPath Is Used in Practice
REST Assured (Java) ships with built-in JSONPath support for API testing assertions.
JMESPath is AWS's answer to JSONPath, used by the AWS CLI's --query flag.
jq is the de facto standard for JSON processing on the command line with its own richer syntax.
You can try JSONPath directly in our JSON Formatter.
Summary
JSONPath gives you XPath-like query power for JSON data. The key operators: $ (root), . and [] (navigation), * (wildcard), .. (recursive descent), array slices [0] / [-1] / [0:3], and filter expressions [?(@.price < 10)].
Ready to test JSONPath expressions on your own data? Paste your JSON into our free JSON Formatter and explore the structure interactively.