How to Write JSONPath Queries: RFC 9535 Filters and Functions by Example

Last updated: · 8 min read

JSONPath is a query language for picking values out of a JSON document: an expression such as $.store.book[0].title starts at the root and walks down step by step. For years the language rested on a blog post and on libraries that disagreed with each other, so the same query could return two different results in two tools. RFC 9535, published in February 2024, standardised its syntax and semantics. The JSONPath Tester & Evaluator runs a query according to that RFC and shows the path and value of every match. This guide covers the selectors, filter expressions and the five standard functions with examples, points out notations that older libraries accept but the standard does not, and states the limits of the tool. The outputs in the examples are the tool's real results.

Formula

Syntax summary (RFC 9535):
$                     root node; every query starts with $
.name   ['name']      name selector: the member of an object with that name
[0]   [-1]            index selector: an array element; negative values count from the end
.*    [*]             wildcard: all children of an object or array
[start:end:step]      slice: start inclusive, end exclusive
[0,2]   ['a','b']     several selectors in one pair of brackets
..name   ..[*]        descendant segment: the node itself and everything below it
[?expr]               filter: the children for which the expression is true
  @ current node, $ root;  ==  !=  <  <=  >  >=  &&  ||  !  ( )
  length(value)  count(nodes)  match(string, pattern)  search(string, pattern)  value(nodes)

Input document (the tool's default document, the bookstore from RFC 9535 §1.5; written compactly here):
{ "store": {
    "book": [
      {"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},
      {"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},
      {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},
      {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}
    ],
    "bicycle": {"color":"red","price":399}
} }

Query 1: $.store.book[?@.price < 10].title

Output 1:
Matching nodes: 2
$['store']['book'][0]['title'] = "Sayings of the Century"
$['store']['book'][2]['title'] = "Moby Dick"

[
  "Sayings of the Century",
  "Moby Dick"
]

Query 2: $.store..price
Matching nodes: 5
$['store']['book'][0]['price'] = 8.95
$['store']['book'][1]['price'] = 12.99
$['store']['book'][2]['price'] = 8.99
$['store']['book'][3]['price'] = 22.99
$['store']['bicycle']['price'] = 399

Query 3: $..book[?@.isbn && @.price > 10].title
Matching nodes: 1
$['store']['book'][3]['title'] = "The Lord of the Rings"

Query 4: $.store.book[?search(@.title, 'of')].title
Matching nodes: 3
$['store']['book'][0]['title'] = "Sayings of the Century"
$['store']['book'][1]['title'] = "Sword of Honour"
$['store']['book'][3]['title'] = "The Lord of the Rings"

A query consists of the root identifier $ followed by segments. Each segment applies its selectors to every node in the nodelist it receives and produces a new nodelist; the result is the nodelist of the last segment, and it may be empty. The first output line is the number of matching nodes; the following lines give the normalized path of each match (in the form $['store']['book'][0], RFC 9535 §2.7) with its value, and the array at the end holds all the values. For Queries 2 to 4 only these first lines are shown.

What the inputs mean

JSON document
The valid JSON text the query runs against; it may be an object, an array or a single value. Up to 1,000,000 characters are processed. For invalid JSON the tool shows the parser's error message.
JSONPath query
A query starting with $. Leading and trailing whitespace is removed. On a syntax error the tool reports the character position and the reason.

Worked example: Titles of the books cheaper than 10 in the bookstore document

  1. Query 1 has four steps: the root $, the members .store and .book, the filter [?@.price < 10] and the member .title. The filter tries the four elements of the book array in order; @ refers to the book being tested each time.
  2. The prices are 8.95, 12.99, 8.99 and 22.99, so the first and third books pass. The result is two nodes: $['store']['book'][0]['title'] = "Sayings of the Century" and $['store']['book'][2]['title'] = "Moby Dick".
  3. The bicycle priced at 399 is not on the query's path at all. For every price, use the descendant segment: $.store..price selects five nodes, the last of which is $['store']['bicycle']['price'] = 399.
  4. Conditions are combined with && and ||. [?@.isbn && @.price > 10] looks for a book that has an isbn member and costs more than 10; @.isbn on its own is an existence test. The result is a single node: "The Lord of the Rings".
  5. An existence test is negated with !: $.store.book[?@.category == 'fiction' && !@.isbn].title returns only "Sword of Honour".
  6. search() checks whether the pattern occurs anywhere in the string: search(@.title, 'of') finds three titles. match() requires the whole string to fit the pattern: match(@.title, 'of') selects no node, whereas match(@.author, '.*Tolkien') finds "The Lord of the Rings".
  7. length() returns the number of characters in a string and has to be compared with a value: $.store.book[?length(@.title) > 15].title returns two titles ("Sword of Honour" is exactly 15 characters long and is left out).
  8. Negative indexes count from the end, but the path is always written with a non-negative index: the path for $..book[-1] is $['store']['book'][3].

Result: Query 1 selects two nodes and the first output line reads: Matching nodes: 2. The array of values is ["Sayings of the Century", "Moby Dick"].

🧭 JSONPath Tester & Evaluator (RFC 9535)

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Queries written before RFC 9535 may fail in this tool; that is a deliberate choice of the standard, not a gap in the tool. Script expressions ([(@.length-1)]), the =~ operator and pseudo-properties such as .length are not part of the standard; their counterparts are a negative index, match()/search() and the length() function. If the library you use speaks a dialect older than the RFC, a query that works here may give a different result there, so check that library's documentation. Two points deserve attention. The first is order: the order of object members is undefined in JSON, and the RFC does not prescribe one for object members in the results of * and ..; this tool usually returns members in document order, but another implementation may not. The second is regular expressions: for match() and search() the RFC specifies I-Regexp (RFC 9485), whereas this tool runs patterns on the JavaScript regular expression engine, so constructs that I-Regexp lacks, such as \d or lookahead, are accepted here but may not work elsewhere. The document is processed in your browser and is not sent anywhere; even so, replacing customer data or access keys with sample data before pasting JSON into any website is a good habit.

Common mistakes

  • Fetching the last element with an old script expression. The query $.store.book[(@.length-1)] is rejected: JSONPath syntax error (position 14): unexpected (. Write $.store.book[-1] instead.
  • Writing a regular expression with the =~ operator. For $.store.book[?@.title =~ /of/] the tool reports: JSONPath syntax error (position 23): ] expected here. Use search(@.title, 'of') instead.
  • Asking for an array's length with .length. The query $.store.book.length raises no error, but it selects no node because there is no member called length; length is only available through the length() function inside a filter.
  • Using match() for a substring search. match() matches the whole string; use search() for an occurrence anywhere, or write the pattern as .*of.*.
  • Using a function without a comparison. For $.store.book[?length(@.title)] the tool reports: JSONPath syntax error (position 15): length() returns a value; it cannot be used as a test on its own, compare it with a value
  • Comparing a number with a string. @.price < '10' is always false; under the RFC, values of different types are never equal to or less than each other. Remove the quotes.
  • Starting the query without $. For store.book the tool reports: JSONPath syntax error (position 1): the query must start with $

Frequently asked questions

How do I write a JSONPath filter? Can you give some examples?

A filter sits inside square brackets, starts with ? and uses @ for the current element. On the default document, $.store.book[?@.price < 10] selects the books cheaper than 10, $.store.book[?@.isbn] those with an isbn member, $.store.book[?@.category == 'fiction' && !@.isbn] the fiction titles without an isbn, and $.store.book[?@.price > 10 || @.category == 'reference'].price three prices (8.95, 12.99, 22.99). The parentheses of the older notation [?(@.price < 10)] are valid as well.

What is the difference between JSONPath and jq?

Broadly, JSONPath is a selection language: it picks nodes out of a document, the result is a list of nodes and the document is not transformed; it is standardised in RFC 9535 and available as a library in many languages. jq is a separate tool and language with its own syntax which, besides selecting, can build new objects, compute and aggregate. The two syntaxes are not interchangeable.

What is the difference between match() and search()?

Both take a string and a pattern and return true or false. match() requires the whole string to fit the pattern; search() requires some part of it to fit. Neither can be used in a comparison; they are written as a test on their own: [?search(@.title, 'of')].

What is a normalized path?

A query that writes the location of one node in exactly one way: brackets only, names in single quotes and non-negative indexes, for example $['store']['book'][3]. It is defined in RFC 9535 §2.7. The tool shows every match in this form, so you can copy a path and use it as a query that selects precisely that node.

What are the limits of the tool?

The document and the query may each be up to 1,000,000 characters long. If a query selects more than 100,000 nodes or exceeds 5,000,000 evaluation steps, the tool stops with the message "Result too large: the query selects too many nodes. Write a narrower query." The output lists at most 1,000 path lines and shortens values longer than 200 characters on those lines; the array below them contains every value in full. Only the five functions from the RFC are supported.

Is my JSON document sent to a server?

No, the query runs in your browser. The safest habit is still to replace confidential data with sample data before pasting a document into an online tool.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice