JSON Path Generator

Finding a value in a document is only half the job. The other half is writing the expression that reaches it in your own code, without a typo, and without it exploding when a field is missing.

Three levels of safety

Selecting a value gives you three versions of the same access, and which one you want depends on how much you trust the data:

  • Direct - the shortest expression. Fastest to read, raises if any step is missing.
  • Safe - returns null or None instead of raising when a step is missing.
  • Guarded - wrapped in the error handling that language actually uses.
Python, for data["articles"][44]["title"]
# direct
data["articles"][44]["title"]

# safe - an out-of-range index yields None rather than raising
((((data or {}).get("articles") or [])[44:45] or [None])[0] or {}).get("title")

# guarded
try:
    result = data["articles"][44]["title"]
except (KeyError, IndexError, TypeError):
    result = None

Code that actually compiles

Each language gets the form that language really uses, not a guess that looks plausible. JavaScript needs ?.[0] before an index and not ?[0], which is a syntax error. org.json’s getJSONArray takes the key it is reading. serde_json’s get returns an Option, so the next step has to go through and_then. Go and Swift cannot express a safe lookup inline at all, so a small helper is emitted with the call.

Keys are escaped for the target language too - PHP gets single quotes so a $ in a key cannot interpolate, Ruby escapes #{, and JSON Pointer escapes ~ and /.

Languages covered

Python, JavaScript, TypeScript, Java, C#, Go, Rust, Ruby, PHP, Kotlin, Swift, and JSONPath expressions for tools that take them.

Questions

How do I get the path to a value in JSON?
Click the value in the tree. The panel on the right shows its path and the code to read it, in whichever language you select.
What is the difference between the safety levels?
Direct is the shortest expression and raises if a step is missing. Safe returns null or None instead. Guarded wraps the access in that language’s error handling. Use direct for data you control and safe or guarded for anything from an API.
Does it support JSONPath?
Yes. Choose JSONPath as the target language to get an expression like $.articles[44].title, with keys quoted where they need to be.

Open JSON Explorer and try it on your own data