A JSON API hands you a document with hundreds of fields, and you need exactly one of them: a title, a price, a status buried three levels down. Reading the whole response and picking the value out by eye works once. The second time, you want a written address for that value, something you can reuse and test: a JSONPath expression.

JSONPath is a small query language for JSON. An expression starts at the root of the document and walks down one step at a time, naming keys and array positions until it lands on the values you want. This guide explains each building block in turn, using the free JSONPath Tester on Ovatly, which runs in your browser against JSON you paste in.

Contents

  • Quick answer
  • The $ root: where every expression starts
  • Dot notation: one key at a time
  • Bracket notation: indexes and awkward key names
  • The wildcard: every item at a level
  • Recursive descent: finding a key at any depth
  • What this tester does not support
  • Worked example: building an expression step by step
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

A JSONPath expression starts with $, the root of the document. Add .key to step into an object, [0] to pick an item from an array by position, and [*] to select every item at a level. Use bracket notation such as ['unit price'] when a key name contains a space or a dot. Use ..key to find a key at any depth. Paste your JSON into the JSONPath Tester, type the expression, and read the matched values. Filters and slices are not supported in this tester, and zero matches is a valid result, not an error.

The $ root: where every expression starts

The $ symbol stands for the root of the document: the whole JSON value you are querying. Every expression begins with it, and on its own $ selects the entire document.

Each step you append narrows the selection. Think of $ as the building and each step as a floor and a room inside it. $.store is one room. $.store.book is a room inside that room. If a step names something that does not exist at that level, the expression matches nothing from that point on.

Dot notation: one key at a time

Dot notation steps into an object by key name. $.store selects the value under the key store; $.store.book goes one level deeper, to the value under book. One dot, one level.

Dot notation works when key names are simple: letters, digits and underscores, with no spaces. It breaks when a key name contains a space or a dot of its own, because the parser reads the space as part of the name boundary and the dot as another step. A key named unit price or release.date cannot be addressed with dots; that is what bracket notation is for.

Bracket notation: indexes and awkward key names

Bracket notation does two jobs. The first is array indexes: [0] selects the first item of an array, [1] the second, and so on.

The second job is key names that dots cannot express. Inside brackets, the key goes in quotes and is taken literally:

$['unit price']
$.store['release.date']
$.store.book[0]

Dots and brackets mix freely in one expression, as the third line shows: dot steps for simple names, a bracket step for the array index. Both forms mean the same thing when the key name is simple.

The wildcard: every item at a level

The [*] step selects every member at that level: every item in an array, or every value in an object. Combined with a step after it, it applies that step to each member in turn.

That combination is the most useful pattern in JSONPath. $.store.book[*].title walks into the store, takes every book in the array, and returns the title of each one. A single-value address becomes a list of values, one per item.

Recursive descent: finding a key at any depth

The ..key step searches downward through the document and returns every value stored under that key name, at any depth below the current point. $..price finds a price at the top level, a price inside an object, and prices inside objects inside arrays, all in one expression.

Recursive descent suits documents whose shape varies, or when you do not know how deep the value sits. The tradeoff: it can return more than you expected, since a key name reused at several levels matches at every one of them.

What this tester does not support

The JSONPath Tester supports a defined subset of the language: the $ root, .key steps, [index], [*], bracket keys, and ..key recursive descent. It does not support filters such as [?(...)], slices such as [0:2], scripts, or unions of several steps.

If an expression contains one of those parts, the tester returns a clear error naming it, instead of guessing. Full JSONPath implementations support more syntax, so an expression copied from another tool's documentation may need rewriting into the subset above.

Worked example: building an expression step by step

Take this small document, the shape of a shop catalogue response:

{
  "store": {
    "book": [
      { "title": "Dune", "author": "Frank Herbert", "price": 9.99 },
      { "title": "Emma", "author": "Jane Austen", "price": 7.50 }
    ],
    "bicycle": { "color": "red", "price": 19.95 }
  }
}

Build the expression one step at a time, running it after each step:

  • $ selects the whole document.
  • $.store selects the store object, holding the book array and the bicycle.
  • $.store.book selects the array of two books.
  • $.store.book[0] selects the first book only, the Dune record.
  • $.store.book[*].title selects the title of every book, and returns Dune and Emma.
  • $..price uses recursive descent and returns all three prices, including the bicycle's, because price appears at more than one depth.

Each step either shows you the structure you expected, confirming the path so far, or returns nothing, telling you the last step is the one to fix.

Limitations and honest tradeoffs

The supported subset cannot select by value: there is no expression here for "books cheaper than 10", because that needs a filter. The workaround is to select the wider set with a wildcard and compare the values yourself.

Zero matches is silent. A misspelled key name returns an empty result rather than an error, so an expression can be wrong without anything looking broken. Check each level against the data as you build.

The tester works on JSON you paste in. It does not fetch a URL or call an API, so the response is copied in first; in exchange, the data never leaves your browser.

Common mistakes

  • Forgetting the leading $. Every expression starts at the root; without it, the first key has nothing to attach to.
  • Using dots for a key with a space or a dot in its name. The parser splits the name at the space or dot. Use bracket notation with quotes.
  • Writing a slice such as [0:2] and expecting the first two items. Slices are not supported here and return an error naming the part.
  • Treating zero matches as a crash. It is a valid result. Shorten the expression level by level until it matches again; the first level that returns nothing holds the mistake.
  • Expecting [*] to fail on an object. The wildcard selects every value in an object as happily as every item in an array.

FAQs

What does the $ symbol mean in JSONPath? The $ is the root of the document. Every expression starts there, and each step after it moves one level deeper: a key into an object, or an index into an array.

When should I use brackets instead of dots? Use brackets when a key name contains a space or a dot, since dot notation reads those as separate steps. Brackets are also how you write array indexes, such as [0] for the first item.

What does [*] do? The wildcard selects every member at that level: every item in an array, or every value in an object. $.store.book[*].title, for example, returns the title of every book in the array.

Are JSONPath filters supported in the tester? No. Filters such as [?(...)] and slices such as [0:2] are outside the supported subset, and so are scripts and unions. The tester returns a clear error naming the unsupported part instead of a wrong result.

What does it mean when an expression returns zero matches? Zero matches is a valid result, not an error. The path is well formed but nothing in the document sits at that location, usually because a key name or an index along the way does not match the data.

Conclusion

Every JSONPath expression is the same idea repeated: start at $, then add one honest step at a time, a key, an index, a wildcard, or a descent, until the selection is exactly what you wanted. To put the pieces to work on a real response, our guide to extracting one field from a JSON response with JSONPath walks through the level-by-level method on a full example. For more guides in this series, see our Data and JSON Tools guides.

References