JSONPath Root Element ($)
Learn how the JSONPath root element $ works. It references the top-level object or array and is the starting point of every JSONPath expression.
Detailed Explanation
The Root Element in JSONPath
The dollar sign $ is the foundation of every JSONPath expression. It refers to the root object or array of the JSON document and serves as the anchor from which all paths are evaluated.
Syntax
$
When you evaluate $ against any JSON document, the result is the entire document itself. Every other JSONPath expression builds on top of this root reference.
How It Works
Consider the following JSON:
{
"name": "Alice",
"age": 30,
"roles": ["admin", "editor"]
}
Evaluating $ returns the complete object. This is useful when you need to pass the full document downstream or confirm the structure before drilling into nested paths.
Key Points
- Every expression starts with
$— even when you write$.name, the$tells the evaluator to begin at the root. - Works with both objects and arrays — if the root is an array like
[1, 2, 3], then$returns that array. - Implicit root — some implementations allow omitting
$, but including it explicitly is best practice for clarity and portability.
Comparison with Other Query Languages
| Language | Root Symbol |
|---|---|
| JSONPath | $ |
| XPath | / |
| jq | . |
Understanding the root element is essential before exploring dot notation, bracket notation, and more advanced operators like recursive descent.
Use Case
Use the root element when you need to reference or return the entire JSON document, such as when piping data between tools, validating top-level structure, or as a starting point before applying filters and projections.