JSONPath

The query language for JSON (similar to XPath for XML), used to select and extract specific data from complex JSON structures.

JSONPath is a standardized query language designed to extract specific elements from a JSON document. If you have a massive, deeply nested JSON file and only need to retrieve a list of specific values (like all the email addresses of users), JSONPath allows you to do this with a single, concise expression instead of writing complex loops in your code.

It serves the exact same purpose for JSON that XPath serves for XML documents.

Key Syntax & Operators

  • $ : The root object or array (always starts a JSONPath expression).
  • . or [] : Child operator to access properties.
  • .. : Deep scan. Recursively searches the entire JSON structure for a matching key.
  • * : Wildcard. Returns all elements regardless of their key or index.
  • [?()] : Filter expression to evaluate a condition (e.g., finding items where price < 10).

Example Usage

Imagine the following JSON payload representing a bookstore:

{
  "store": {
    "books": [
      { "category": "reference", "price": 8.95, "title": "Sayings of the Century" },
      { "category": "fiction", "price": 12.99, "title": "Sword of Honour" }
    ]
  }
}

Using JSONPath, you can query this data dynamically:

  • Query: $.store.books[*].title
    Result: ["Sayings of the Century", "Sword of Honour"]
  • Query: $.store.books[?(@.price < 10)].title
    Result: ["Sayings of the Century"]

JSONPath is heavily used in API testing tools, data extraction pipelines, and cloud infrastructure routing (like AWS Step Functions).