JSONPath Cheat Sheet: Syntax & Examples to Query JSON

The JSONPath operators in one table, worked query examples, how to filter by condition, and where JMESPath fits in.

You have a big JSON response and you need one slice of it — every product over $10, or just the email addresses buried three levels down. Looping in code works, but a JSONPath expression does it in a single line. JSONPath is to JSON what XPath is to XML: a query language for reaching into a document and pulling out exactly what you want. Here's the syntax, with examples you can copy.

What is JSONPath?

JSONPath is a query language for JSON. You write an expression that starts at the document root ($) and navigates down into objects and arrays. It was introduced by Stefan Goessner in 2007 and finally standardized as RFC 9535 in 2024 — which matters because older implementations predate the standard and differ on a few edge cases.

JSONPath syntax cheat sheet

These operators cover almost everything you'll write:

SyntaxMeaningExample
$The root element$
.key or ['key']A child property$.store.bicycle
..keyRecursive descent — find key at any depth$..price
*Wildcard — all children$.store.book[*]
[n]Array index ([0] first, [-1] last)$.store.book[0]
[a:b]Array slice (start inclusive, end exclusive)$.store.book[0:2]
[a,b]Union — several indices or keys at once$..book[0,2]
[?(@.x > 1)]Filter — @ is the current element$..book[?(@.price < 10)]

JSONPath by example

Take this classic “bookstore” document:

{
  "store": {
    "book": [
      { "title": "Sayings of the Century", "author": "Nigel Rees", "price": 8.95 },
      { "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 }
  }
}

Now the queries and what each returns:

JSONPathReturns
$.store.book[*].titleAll three book titles
$..priceEvery price anywhere: 8.95, 8.99, 22.99, 19.95
$.store.book[0]The first book object
$.store.book[-1].title"The Lord of the Rings" (last book)
$.store.book[0:2]The first two books

Filtering by condition

The real power of JSONPath is the filter expression [?( … )], where @ refers to the element currently being tested:

$..book[?(@.price < 10)]            // books cheaper than 10
$..book[?(@.price > 10)].title      // titles of the expensive ones
$..book[?(@.author == "Herman Melville")]

One portability note: most libraries (and the original Goessner syntax) write filters as [?(@.price < 10)] with parentheses, while RFC 9535 also allows [?@.price < 10] without them. If a filter “doesn't work,” it's usually this difference — check what your library expects.

JSONPath vs JMESPath

JSONPath isn't the only JSON query language. JMESPath is a separate, strictly specified language you've probably used without realizing — it powers the AWS CLI's --query flag. The same “all book titles” query looks different in each:

JSONPath:  $.store.book[*].title
JMESPath:  store.book[*].title

JSONPath filter:  $..book[?(@.price < 10)]
JMESPath filter:  store.book[?price < `10`]

Neither is “better” — pick the one your ecosystem expects (JMESPath for AWS tooling; JSONPath almost everywhere else). Our query tool supports both, so you can paste an expression in either syntax and see the result.

Using JSONPath in code

In JavaScript, jsonpath-plus is a common choice:

import { JSONPath } from "jsonpath-plus";

const titles = JSONPath({ path: "$.store.book[*].title", json: data });
const cheap  = JSONPath({ path: "$..book[?(@.price < 10)]", json: data });

In Python, jsonpath-ng does the same:

from jsonpath_ng.ext import parse

expr = parse("$.store.book[*].title")
titles = [m.value for m in expr.find(data)]

Test JSONPath queries online

When I'm working out the right expression, I don't guess in code — I iterate against real data first. Our free JSONPath & JMESPath query tool lets you paste JSON, type an expression in either language, and see the matches instantly, fully in your browser (disclosure: I built it). To just find where a value lives before writing the path, JSON Search helps, and the JSON formatter makes the structure easy to read.

FAQs

What is JSONPath?
A query language for JSON, inspired by XPath. Expressions start at the root ($) and navigate in — e.g. $.store.book[*].title. It was standardized as RFC 9535 in 2024.

What's the difference between JSONPath and JMESPath?
Two different query languages. JSONPath uses $, .., and [?()] filters; JMESPath is a stricter spec used by the AWS CLI. The query tool supports both.

How do I filter a JSON array with JSONPath?
Use [?(@.field < value)] with @ for the current element, e.g. $..book[?(@.price < 10)].

How do I select the last array element?
[-1] in most implementations, or a slice like [-2:] for the last two.

Is JSONPath standardized?
Yes — RFC 9535 (2024). Older libraries predate it and vary on edge cases, so test against the implementation you use.

Related articles