Closed antstei closed 7 years ago
I wonder now why and when a ^ should be used.
^
can be used in case of you need to locate something which is in another part of your data, for instance:
.a.b.c{.d === ^.e.f}
Thanks :+1:
But AFAIK a JSON object may only have one root element—why does the ^
not return this document root?
why does the ^ not return this document root?
I'm not quite sure what you mean. ^
points to root.
^
points to root.
When you think of the above example JSON data book
is the document root.
But unfortunately ^
returns the entire JSON object—since for example a ^.author
query yields an empty list—rather than a "pinter" to its root element book
.
Instead the ^
should return the same data as a .*
query does IMO… :smile:
There could be more than one key on the top level:
var doc = {
"books" : [...],
"authors" : [...]
};
So I don't see how ^
could point to books
.
Oops! You are completely right! I mixed up JSON and XML. Yes a JSON structure could be and is usually "anonymous" and doesn't necessarily have a "root member object".
Back to my question; the ^
is used to locate a property when another context (root) is already used in the location path and / or object predicate?! As the following code demonstrates:
var doc = {
"books" : {"a": 1},
"authors" : {"b": 1}
};
JSPath.apply('.books{.a === ^.authors.b}', doc)
// yields [Object { a=1}]
Many thanks for your support and this great project :smile:
the ^ is used to locate a property when another context (root) is already used in the location path and / or object predicate?!
Yes, it is.
The documentation says that
But all examples use the leading
.
—which locates a context items itself—at the beginning of an absolute path:instead of
which leads to an syntax error since
^
expects a trailing popery starting with a.
.Therefore
yields the expected data.
I wonder now why and when a
^
should be used.Thanks :smile: