Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for literal * keys return all when * unavailable #77

Closed
micimize opened this issue Dec 12, 2016 · 6 comments
Closed

Searching for literal * keys return all when * unavailable #77

micimize opened this issue Dec 12, 2016 · 6 comments
Labels

Comments

@micimize
Copy link

Searching for a literal * with $['*'] yields the following unexpected behavior:

JSONPath({json: {'*': 'star', 'rest': 'rest', 'foo': 'bar'}, path: "$['*']"})
//=> [ 'star' ]
JSONPath({json: {'rest': 'rest', 'foo': 'bar'}, path: "$['*']"})
//=> [ 'rest', 'bar' ]

It's possible to work around this with $[?(@property === '*')], but this isn't usable in more complex queries, such as $[?(@property === '*'), rest]

@micimize micimize changed the title Searching for literal * keys return all when * unavailable Searching for literal * keys return all when * unavailable Dec 12, 2016
@micimize
Copy link
Author

Actually this also applies in reverse:

JSONPath({json: {'*': 'star', 'rest': 'rest', 'foo': 'bar'}, path: "$.*"})
//=> [ 'star' ]

brettz9 added a commit to brettz9/JSONPath that referenced this issue Dec 12, 2016
…scaped (breaking change);

Refactoring (group similar together)
brettz9 added a commit to brettz9/JSONPath that referenced this issue Dec 12, 2016
…scaped (breaking change);

Refactoring (group similar together)
@brettz9
Copy link
Collaborator

brettz9 commented Dec 12, 2016

I've added a commit to master which adds the breaking change to always treat special characters as such unless a backtick (`) is added at the beginning in which case it will always treat as a literal.

If this will end up as a problem, I can revert, but I think this behavior makes the most sense and is the safest.

@micimize
Copy link
Author

Thanks for the quick response! This works in the flat structures, but leads to some unexpected results with more complex nested structures:

let json = {'*': 'star', 'foo': 'bar', 'sibling': { child: 'sibling child', '*': 'sibling star'}}

// earlier tests work as expected
JSONPath({ json, path: "$[foo,`*]" })
// => [ 'bar', 'star' ]

// sibling subqueries ignore `*
JSONPath({ json, path: "$[`*,sibling[child,`*]]" })
// => [ 'sibling child', 'sibling star' ]

// ordering of subqueries queries changes results
JSONPath({ json, path: "$[sibling[child,`*],`*]" })
// => [ 'sibling child', 'sibling star', 'sibling star' ]

I'm also curious as to why you chose ` instead of \, which seems more intuitive to me

@brettz9
Copy link
Collaborator

brettz9 commented Dec 13, 2016

The current parsing is regex based. I think we'd need to move to something more robust to deal properly with the nesting. I also don't have a lot of time to work on this project atm. Feel free to submit any PRs to handle though (along with your test cases).

As far as the backtick, I just used that (along the lines of templates, etc.) because I hadn't provided support for escaping of individual characters but rather for the whole sequence.

@micimize
Copy link
Author

ah, I see - the , operator only works in the tail position of a path, making that issue more related to #66
That's what I thought with the syntax decision - makes sense 👍

@brettz9
Copy link
Collaborator

brettz9 commented Jan 14, 2017

FYI, the feature is included with v0.16.0, now released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants