What is the JSONPath syntax and how do I use it?

JSONPath uses a concise syntax with special operators to navigate and filter JSON data.

Basic Operators:

$ - Root object/element (all paths start with this)

$ → Selects the entire document

. - Child operator (dot notation)

$.store → Selects the store property
$.store.name → Selects nested property

[] - Array subscript or property access

$.books[0] → First book
$.books[1,3,5] → Books at index 1, 3, and 5
$['property-with-dash'] → Property with special characters

* - Wildcard (matches all elements)*

$.books[*] → All books
$.books[*].title → All book titles

.. - Recursive descent (search all levels)

$..author → All authors at any depth
$..price → All prices anywhere in document

Array Slicing:

$.books[0:3] → First three books (index 0, 1, 2)
$.books[-2:] → Last two books
$.books[:5] → First five books
$.books[::2] → Every other book

Filter Expressions ?( ):

$.books[?(@.price < 10)] → Books under $10
$.books[?(@.inStock === true)] → Books in stock
$.books[?(@.author === "John")] → Books by John
$.books[?(@.price > 10 && @.inStock)] → Multiple conditions

Special Characters in Filters:

  • @ - Current node being processed
  • == - Equals
  • != - Not equals
  • <, >, <=, >= - Comparisons
  • && - AND
  • || - OR
  • ! - NOT

Practical Examples:

$.users[*].email           // All user emails
$..products[?(@.price < 20)] // All products under $20
$.data.items[0:10]         // First 10 items
$..comments[*].author      // All comment authors
$.api.response[?(@.status === 'success')] // Successful responses

Related Questions