dfilatov / jspath

DSL that enables you to navigate and find data within your JSON documents
MIT License
553 stars 40 forks source link

[Closed] Why can absolute location paths start with a "." instead of a "^"? #49

Closed antstei closed 7 years ago

antstei commented 8 years ago

The documentation says that

If location path starts with the root (^) you are using an absolute location path—your location path begins from the root items.

But all examples use the leading .—which locates a context items itself—at the beginning of an absolute path:

//The JSON data…
var doc = {
    "books" : [
            {
            "id"     : 1,
            "title"  : "Clean Code",
            "author" : { "name" : "Robert C. Martin" },
            "price"  : 7
        },
        {
            "id"     : 2,
            "title"  : "Maintainable JavaScript",
            "author" : { "name" : "Nicholas C. Zakas" },
            "price"  : 12
        },
        {
            "id"     : 3,
            "title"  : "Agile Software Development",
            "author" : { "name" : "Robert C. Martin" },
            "price"  : 15
        }
    ]
};
// and the query to find all books authors

JSPath.apply('.books.author', doc);
/* [{ name : 'Robert C. Martin' }, { name : 'Nicholas C. Zakas' }, { name : 'Robert C. Martin' }, { name : 'Douglas Crockford' }] */

instead of

console.log(JSPath.apply('^book.author', doc));

which leads to an syntax error since ^ expects a trailing popery starting with a ..

Therefore

console.log(JSPath.apply('^.books.author', doc));

yields the expected data.


I wonder now why and when a ^ should be used.

Thanks :smile:

dfilatov commented 8 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}
antstei commented 8 years ago

Thanks :+1: But AFAIK a JSON object may only have one root element—why does the ^ not return this document root?

dfilatov commented 8 years ago

why does the ^ not return this document root?

I'm not quite sure what you mean. ^ points to root.

antstei commented 8 years ago

^ 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:

dfilatov commented 8 years ago

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.

antstei commented 8 years ago

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:

dfilatov commented 8 years ago

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.