Back to blog
Guides3 min readVektosys

JSONPath Explained: Query JSON Without Writing Loops

Learn JSONPath syntax, common selectors, practical examples, and how path queries speed up exploration of nested documents.

The problem JSONPath solves

Nested JSON is easy to produce and annoying to poke through manually. JSONPath gives you a concise query language—similar in spirit to XPath for XML—to select nodes from a JSON document. Instead of writing nested loops, you express “give me every SKU under items” as a path.

Implementations vary slightly by library, but the core ideas are shared across languages and tools.

Root and basic selectors

Most expressions start with $ (the document root).

Given:

{
  "store": {
    "book": [
      { "title": "Lean JSON", "price": 10 },
      { "title": "API Design", "price": 15 }
    ],
    "bicycle": { "color": "teal", "price": 50 }
  }
}

Examples:

  • $.store.bicycle.color"teal"
  • $.store.book[0].title"Lean JSON"
  • $.store.book[*].price10 and 15

* is a wildcard. [index] picks array elements. Dot notation walks object keys.

Recursive descent

.. searches descendants:

  • $..price → every price anywhere in the tree
  • $..book[1] → the second book object wherever book arrays appear

Recursive queries are powerful for discovery and dangerous for huge documents—prefer narrower paths in production code.

Filters

Many JSONPath dialects support filter expressions:

$.store.book[?(@.price < 12)].title

This returns titles of books cheaper than 12. Filter syntax is one of the least standardized areas—verify behavior in your library before relying on it in CI.

Practical use cases

  • Extract fields from large webhook payloads while debugging
  • Build assertions in API tests (“all items have sku”)
  • Power admin search UIs over JSON configs
  • Prototype mappings before writing permanent transformation code

Interactive explorers help you learn by doing. Paste a document, try a path, refine. JSON Editor includes JSONPath utilities so you can validate selectors against real samples quickly.

JSONPath vs. application code

Use JSONPath for exploration, tests, and lightweight extractions. For core business logic, typed parsing into structs/classes is usually safer: compilers and type checkers catch renames that string paths miss. Treat paths as contracts that need tests.

Common pitfalls

  • Assuming every implementation supports the same filter operators
  • Forgetting that keys with special characters may need bracket notation: $['odd.key']
  • Expecting guaranteed order from object key iteration across all runtimes
  • Running $..* on multi-megabyte logs and wondering why the UI freezes

Learning path

  1. Master $, dots, and indexes.
  2. Add wildcards for arrays of uniform objects.
  3. Try recursive descent on a small sample.
  4. Introduce filters only when your library’s docs are open beside you.
  5. Freeze successful paths into tests.

JSONPath will not replace thoughtful data modeling, but it removes friction when you need answers from a nested document now. Learn a handful of patterns and keep a playground tab ready for the next opaque payload.

Related articles