Some JSON documents are tidy: every product sits at the same depth, every field in the same place. Real documents are often not. One branch nests its items inside a wrapper, another does not, and the key you need turns up at depth three in one place and depth five in another. An explicit path written for the tidy version reads the tidy branch and silently misses the rest.

Recursive descent is the JSONPath operator built for uneven documents. Written as two dots before a key, it searches the whole subtree and collects every match, wherever it sits. This guide explains how it behaves and when to prefer it over a full path, using the free JSONPath Tester on Ovatly, which runs in your browser.

Contents

  • Quick answer
  • Why explicit paths miss things
  • What recursive descent actually does
  • Worked example: one key, two depths
  • Recursive descent vs the explicit path
  • How to use recursive descent, step by step
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

To find a key at any depth in a JSON document: write .. followed by the key name, for example $..title, in the JSONPath Tester. The tester searches every level below the root and returns every value stored under that key, from all depths, in one list. Use it when nesting varies or you do not know the structure. Use an explicit path instead when the structure is fixed and you want exactly one location.

Why explicit paths miss things

An explicit path names every level it takes: $.catalog.sections[0].books[*].title enters the catalog, takes the first section, enters its books array, and reads the title of each book. Every step has to match exactly. If a second section wraps its books one level deeper, in a subsection, the path stops matching at that branch and those titles never appear in the result. Nothing reports the miss; the list just comes back shorter.

An explicit path also cannot see sideways. A key sitting shallower than the route, or in a sibling branch the route never enters, is invisible to it. That precision is a virtue when the structure is known and fixed. It is a liability when the structure is the thing you are trying to discover, or when feeds arrive in slightly different shapes.

What recursive descent actually does

..key is an instruction to search, not a route. Starting wherever the expression stands, the tester looks through every object in the subtree, at every depth, and collects the value of that key wherever it appears. The result is a single list of matches from all levels, in the order the search encounters them.

The search can start at the root, as $..title does, or partway down. $.catalog..title searches only inside the catalog object, which is how you keep the reach of descent while excluding branches you know are irrelevant. In the JSONPath Tester, recursive descent is part of the supported subset alongside dot access, indexes, the wildcard and bracket keys.

Worked example: one key, two depths

Here is a small catalogue where the key title lives at two different depths:

{
  "catalog": {
    "title": "Autumn catalogue",
    "sections": [
      {
        "name": "Fiction",
        "books": [
          { "title": "Dune", "price": 12.99 },
          { "title": "Emma", "price": 9.5 }
        ]
      }
    ]
  }
}

The catalogue's own title sits two levels below the root. The book titles sit five levels down. One expression finds all three:

$..title

It returns:

"Autumn catalogue", "Dune", "Emma"

Now compare the explicit path:

$.catalog.sections[0].books[*].title

That returns only Dune, Emma. Neither answer is wrong; they answer different questions. The explicit path answers "what are the titles of the books in the first section?" The descent answers "what is stored under the name title, anywhere in this document?" Pick the expression that matches the question you are asking.

Recursive descent vs the explicit path

Reach for an explicit path when the structure is fixed and stable, and when you want one exact location: the price of the third product, the name of the first section. An explicit path is self-documenting, and it cannot drift into branches you did not name.

Reach for recursive descent when the structure varies between documents, when a wrapper appears in some branches and not others, or when you are exploring a new document and want to know whether a key exists at all. Descent trades precision for reach. The cost: the key name is the only criterion, so every branch that uses the same name contributes a match, wanted or not.

A practical pattern is to explore with descent, then pin down with a path. Find the key with $..key, note how many matches come back, then write the explicit path for the branch you care about and use that in anything you run twice.

How to use recursive descent, step by step

  1. Open the JSONPath Tester and paste your JSON.
  2. Write $.. followed by the exact key name, for example $..title. Key names are matched as written, so copy the spelling from the document.
  3. Read the result list and count the matches. Compare the count with expectations; a surprise in either direction tells you something about the document.
  4. If the list includes matches from branches you do not need, narrow the search by starting it below a known parent, such as $.catalog..title, or switch to an explicit path for the branch that matters.
  5. If the list is empty, treat it as information, not failure: the key is absent, or spelled differently from your expression.

Limitations and honest tradeoffs

Descent finds keys by name only. This tester does not support filters, so there is no way to ask for titles only where the price is under 10; the descent collects every title and leaves the judging to you. With thousands of matches, that list is a starting point for a more specific tool, not a final answer.

The result is a list of values, and it does not label which branch each value came from. When location matters, when you need to know whether a match came from the fiction section or the banner object, follow up with explicit paths into suspect branches. Descent tells you the key exists and what it holds; it does not draw you a map.

Zero matches is a valid result, not an error, and unsupported syntax is the opposite: a filter, slice, script or union produces a clear error naming the part the tester cannot run. An empty list means the search ran and found nothing, so check the key name first.

Common mistakes

  • Using descent when an exact path was meant. The extra matches from other branches then look like bad data, when the expression searched wider than intended.
  • Writing a single dot and expecting a search. .title looks at the immediate level only. The search behaviour needs the double dot.
  • Forgetting the leading $. Descent still needs a starting point; $..key starts the search at the root of the document.
  • Treating zero matches as an error. An empty result means the key is absent or misspelled in your expression, not that the search failed.
  • Trusting the count without checking it. Descent collects from every branch, so compare the match count with the document before you use the values.

FAQs

What does the double dot do in JSONPath? It is recursive descent: ..key searches every level below the starting point for that key and collects every value under it, from all depths, into one list. $..title finds every title in a document, whether it sits two levels down or six.

When should I use recursive descent instead of a full path? Use it when the nesting varies between branches or you do not know the structure yet. Use an explicit path when the structure is fixed and you want exactly one location, because an explicit path cannot collect matches from other branches.

Can recursive descent return too much? Yes. It collects every occurrence of the key name in the subtree, including ones in unrelated branches, such as a title on a banner object and the book titles you wanted. Count the results and, if there are extras, narrow the search or switch to an explicit path.

What does it mean when $..key returns nothing? Zero matches is a valid result, not an error. The key is either absent from the document or spelled differently from the name in your expression. Check the exact spelling in the document before concluding the data is not there.

Is my JSON uploaded to a server? No. The tester runs fully in your browser and the JSON never leaves your device. You can disconnect from the internet after the page loads and the tool still works.

Conclusion

Recursive descent turns a route into a search: two dots, a key name, and every match in the document comes back in one list, at whatever depth. The JSONPath Tester makes it a safe way to explore an unfamiliar feed before committing to exact paths. For the everyday counterpart, collecting one field from every item in a list, see the companion guide on pulling prices from a product JSON with JSONPath. For more guides, see our Data and JSON Tools guides.

References