Back to Blog
·10 min read

JSONPath: A Complete Guide

Learn how to query JSON documents using JSONPath expressions with practical examples.

JSONPath is to JSON what XPath is to XML — a query language for navigating and extracting data from JSON documents. It's widely used in tools like Kubernetes, AWS CloudFormation, and many API testing frameworks.

The Basics

Every JSONPath expression starts with $ which represents the root of the document. From there you navigate using dot notation or bracket notation.

// Sample document
{
  "store": {
    "books": [
      { "title": "Moby Dick", "price": 8.99, "category": "fiction" },
      { "title": "Clean Code", "price": 29.99, "category": "tech" },
      { "title": "The Odyssey", "price": 12.99, "category": "fiction" }
    ],
    "location": "New York"
  }
}

Core Operators

  • ·$ — root element
  • ·. or [] — child operator: $.store.location or $["store"]["location"]
  • ·.. — recursive descent (searches all levels): $..price
  • ·* — wildcard (all elements): $.store.books[*].title
  • ·[n] — array index: $.store.books[0]
  • ·[start:end] — array slice: $.store.books[0:2]
  • ·[-1] — last element: $.store.books[-1]

Filter Expressions

Filter expressions use the ?() syntax with @ referring to the current node. They're the most powerful part of JSONPath.

// Books under $15
$.store.books[?(@.price < 15)]

// All fiction books
$.store.books[?(@.category == "fiction")]

// Books that have a price field
$.store.books[?(@.price)]

// Books over $10 in the tech category
$.store.books[?(@.price > 10 && @.category == "tech")]

Recursive Descent

The .. operator searches through all levels of nesting, making it useful when you don't know the exact path to a field.

// Get all prices at any depth
$..price  // [8.99, 29.99, 12.99]

// Get all title values anywhere in the document
$..title

Common Use Cases

  • ·Extracting specific fields from API responses before displaying them
  • ·Filtering arrays by conditions in configuration files (Kubernetes, Helm)
  • ·Writing assertions in API tests (Postman, REST-assured)
  • ·Transforming JSON in ETL pipelines

Tip: Try all these expressions interactively in our JSONPath Finder tool — paste your JSON, type an expression, and see results in real time.

Ad