Back to Blog

JSONPath Expressions: Master JSON Querying

Mar 2, 20266 min read

JSONPath is a query language for JSON, analogous to XPath for XML. Instead of manually traversing object properties in code, you write a compact expression like $.users[*].email and let the JSONPath engine extract exactly what you need. Test your expressions live in our JSONPath Tester.

Core Syntax at a Glance

OperatorMeaningExample
$Root of the document$
.Child accessor$.name
..Recursive descent (all depths)$..email
*Wildcard (all children)$.users[*]
[n]Array index$.items[0]
[start:end]Array slice$.items[1:3]
[?(...)]Filter expression$.users[?(@.age > 18)]

Basic Queries

Sample JSON
{
  "store": {
    "books": [
      { "title": "Refactoring",    "author": "Fowler", "price": 45 },
      { "title": "Clean Code",     "author": "Martin", "price": 35 },
      { "title": "The Pragmatic",  "author": "Thomas", "price": 40 }
    ],
    "location": "New York"
  }
}
  • $.store.location"New York"
  • $.store.books[0].title"Refactoring"
  • $.store.books[*].author["Fowler", "Martin", "Thomas"]
  • $..price[45, 35, 40] (recursive — finds all price keys at any depth)

Filter Expressions

Filter expressions use [?(condition)] to select only elements that match a predicate. The @ symbol refers to the current node being evaluated:

  • $.store.books[?(@.price < 40)] — books cheaper than $40
  • $.store.books[?(@.author == 'Martin')] — books by a specific author
  • $.store.books[?(@.title)] — books that have a title field

Recursive Descent

The .. operator traverses the entire tree recursively. This is powerful for extracting a field from deeply nested or inconsistently structured data:

$..email — finds every email field anywhere in the document, no matter how deeply nested.

Array Slicing

Like Python list slices, JSONPath supports [start:end:step]:

  • $.books[0:2] — first two books (index 0 and 1)
  • $.books[-1:] — last book
  • $.books[::2] — every second book

Real-World Use Cases

  • API response parsing: Extract specific fields from large payloads without deserialising the entire object.
  • Configuration queries: Find all timeout settings across a deeply nested config file.
  • Data transformation: Select arrays of values to feed into a mapping or reduction step.
  • Testing: Assert that a specific path in an API response matches an expected value.

Browser-Based Testing

Use our interactive JSONPath Tester to experiment with expressions against your actual JSON. Results update in real time, and invalid expressions show a clear error message.

Test JSONPath Expressions Live

Paste your JSON and test any expression instantly. Results update in real time.