JSONPath Syntax: Query and Filter JSON (with Examples)
Master JSONPath, the query language for JSON standardized in RFC 9535. Learn root, wildcard, recursive descent, array slices, filter expressions, and functions, with practical examples and tool tips.
JSONPath is to JSON what XPath is to XML: a concise query language for reaching into a document and pulling out exactly the values you want. It is everywhere — Kubernetes kubectl output, API gateways, testing assertions, and data pipelines — and as of 2024 it is finally standardized as RFC 9535. This guide walks through JSONPath syntax from the root symbol to filter expressions and functions, with examples you can adapt immediately.
What is JSONPath?
JSONPath is a string expression that selects nodes from a JSON document. You write a path that navigates objects and arrays, and the engine returns every value that matches. It turns multi-line traversal code into a single readable expression, which is why it shows up in CLI tools, config systems, and test frameworks.
For years JSONPath was a loose convention with subtle differences between implementations. RFC 9535, published in 2024, standardizes the syntax and semantics, so expressions now behave consistently across compliant tools.
Root, dot, and bracket notation
Every JSONPath begins with $, the root of the document. From there you navigate with dot notation or bracket notation. $.store.book selects the book value inside store; the equivalent bracket form is $['store']['book']. Use brackets when a key contains spaces or special characters that dot notation cannot express.
Bracket notation also selects array elements by index: $.store.book[0] is the first book, and negative indexes count from the end, so [-1] is the last element.
$.store.book[0].title
$['store']['book'][0]['title']Wildcards and recursive descent
The wildcard * matches all elements at a level: $.store.book[*] selects every book in the array, and $.store.* selects every value directly under store. The recursive descent operator .. searches all levels of the document. $..author finds every author field no matter how deeply it is nested — invaluable when you do not know or do not care about the exact path.
Array slices and indexes
- $.items[0] — the first element
- $.items[-1] — the last element
- $.items[0:2] — a slice: elements at index 0 and 1 (the end is exclusive)
- $.items[*] — every element
- $.items[0,2,4] — a union of specific indexes
Filter expressions
Filters are where JSONPath becomes powerful. A filter [?<expression>] keeps only the elements for which the expression is true, using the current node identifier @ to refer to each candidate. For example, $.store.book[?@.price < 10] returns every book whose price is under ten.
Filters support the comparison operators ==, !=, <, >, <=, and >=, plus the logical operators && (and), || (or), and ! (not). A bare query inside a filter is an existence test: $.store.book[?@.isbn] selects only books that have an isbn field. Combine them — $.book[?@.price < 20 && @.inStock] — to express precise conditions.
$.store.book[?@.price < 10]
$.store.book[?@.price < 20 && @.inStock]
$.store.book[?@.isbn] (existence test)Functions
RFC 9535 defines an extensible set of functions you can use inside filters. length() returns the size of a string, array, or object; count() returns the number of nodes a query selects; match() tests a value against a full regular expression; and search() tests whether a regex matches anywhere in a string. For example, $.users[?length(@.name) > 10] selects users whose name is longer than ten characters.
A worked example
Consider a bookstore document with a store object containing a book array and a bicycle. $..price selects every price in the document, including the bicycle's. $.store.book[*].author lists all authors. $.store.book[?@.price < 10].title returns the titles of cheap books only. $.store.book[-1] grabs the last book. Building these up incrementally — start broad, then add filters — is the fastest way to land on the exact selection you need.
$..price → every price in the document
$.store.book[*].author → all authors
$.store.book[?@.price < 10].title → titles of cheap books
$.store.book[-1] → the last bookJSONPath versus jq, JMESPath, and JSON Pointer
JSONPath is one of several JSON query tools, and choosing the right one matters. jq is a full transformation language with its own syntax, ideal for reshaping data on the command line. JMESPath, used heavily by the AWS CLI, is another expression language with strong projection support. JSON Pointer (RFC 6901) is much simpler — it identifies a single value by path but cannot filter or wildcard. Reach for JSONPath when you want readable querying and filtering across a document; reach for jq when you need to transform and rebuild it.
Frequently asked questions
Is JSONPath a standard?
Yes, as of 2024. RFC 9535 standardizes JSONPath syntax and semantics. Before that, JSONPath was a widely used convention with implementation differences; compliant tools now behave consistently.
What is the difference between jq and JSONPath?
JSONPath is a query language for selecting nodes from a document. jq is a complete transformation language that can select, filter, reshape, and compute over JSON. Use JSONPath for readable extraction and filtering; use jq when you need to transform data into a new structure.
How do I select items by a condition in JSONPath?
Use a filter expression with the current-node identifier @. For example, $.products[?@.price < 50] selects products under fifty, and you can combine conditions with && and ||.
How do I get every value of a key regardless of nesting?
Use the recursive descent operator. $..email returns every email field anywhere in the document, no matter how deeply nested it is.
What is the difference between JSONPath and JSON Pointer?
JSON Pointer (RFC 6901) addresses a single value by an exact path and cannot filter or use wildcards. JSONPath can select many nodes, wildcard across levels, and filter by condition, which makes it far more expressive for querying.
Related tools
Try these tools
JSON Path Extractor
Query JSON with dot paths and bracket indices — extract values at any key path.
JSON Pick & Omit Keys
Select or remove top-level keys from a JSON object — whitelist or blacklist fields.
JSON Viewer & Tree Explorer
Explore JSON with an interactive collapsible tree view.
JSON Formatter & Beautifier
Format, prettify, and beautify messy JSON with configurable indentation and key sorting.