-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Filter expression return array can not use [index] get item inside #272
Comments
Somehow i have a similar issue here openhab/openhab1-addons#4768 i there any way to address the item after a filtering has been done? |
I found out a possible solution:
would take the one value from the list... |
A path must point to something in the document. That is the case for:
but not with:
where the [0] actually is expected to be applied to the result of the path evaluation. I agree that this would useful in many situations but it should not be confused with the actual path. |
My 2 cents.
Hope it could help until we will be able to do sort of: |
Any chance of getting a way to support this? Would be a nice to have. |
@jochenberger whats your thoughts on this? |
That's a tough one. It's apparently not part of the original JsonPath spec and is not supported on any of the implementations. |
I would agree that going off spec is not the best idea yes because then you have an excuse to just add anything even if it deviates spec. Maybe custom functions or some sort of extension capability which is separate from the base project (which is pure spec). |
What are the contribution guidelines in terms of "accepting any terms" or processes. I thought I might try experimenting. |
I don't think there are any terms. Adding tests is a good way to get PRs merged, so is not breaking existing ones. ;-) |
…ve selection, needed as workaround for: json-path/JsonPath#272
Struggeling with the same and @kallestenflo have a hard time to understand your argument:
The result of the path operation after the filter is a JSONArray, so at that point he document is a JSON Array, e.g. with 1 element. Let's study further based on my current real world example, parsing cloud foundry environment information. Realworld example, a typical vcap_service cloudfoundry env variable value: And we need to access the credentials, e.g. the hostname: Currently this returns a JSONArray of 1 element so I tried: and $.*[?(@.name == 'my-persistence')].credentials.hostname[0] to get a clean String value returned, but no luck due to this issue. In my view after: The 'intermediate document' that the next operator is applied to is: (because it is valid to access $.*[?(@.name == 'my-persistence')].credentials.hostname which returns [ "10.11.12.133" ] ) So why not allow the path to navigate to [0], after which the intermediate document is: then naviate to credentials.hostname and get a clean string value "10.11.12.133" ? |
@jochenberger and @kallestenflo is there any support on this. Filtering should not lead to a non accessible array. |
Good to see I am not the only one struggling with this. I would expect such a filter to return whatever the content type is, not forced in a single result array. My workaround is to parse the one result to a string and either add the following to the assert somewhere: Is it because you stay with the content type list and filter inside that? In which case I would have to say I see why you went with a single entry list and I will alter my approach to use something like what I found at @fhoeben 's commit and use Yeah, it all makes a lot more sense now. Still would like it to return the object type of the actual object referenced to. |
How to get he array name? My json data is as below I am looking for list of store only i.e. in above case output should be just "book" For this what should be json path . |
I know that this is not the same as being able to select any given element, but I think a lot of people end up here because they are looking for a way to select the first (or maybe last) element of the resulting array from an applied filter. Therefore I don't think we should necessarily go for:
And expect a specific element of the array, because square brackets, for JSON path, is either square bracket notation or applying a filter (and there's nothing in the spec that specifically covers this use case where we want to mix both). I would argue the closest way to adhere to the spec is to add some more methods, called after the filter, the same way we do for It's not as powerful but could be easier to implement and resolve a number of people's issues? |
I'm having problems understanding how come a filtered array is not an array, that is without any knowledge of library internals. |
Any update on this issue? |
Hey! |
Apparently there is no workaround in a single path selector. The workaround is to read in 2 stages. |
I had to build my own parser because of this issue, it was surprisingly easy with ANTLR4 |
@zakjan any chance you published that parser? Maybe others could benefit also. |
@fhoeben Yeah, I'll try to extract it and share |
Hi @zakjan I'd be interested in seeing this too if you don't mind sharing? Thanks in advance. |
I like @apocheau workaround suggestion (it less hacky) but I think this might also be a good one:
then
It eliminates all the |
My workaround for kotlin applications is to extend DocumentContext with a function to read a String directly, like this:
Then it can be used like this:
EDIT: Improved to handle empty results |
@fhoeben @bhreinb Sorry for late response. My parser is already published at https://github.com/zakjan/objectpath . It supports more advanced cases, might be too complex for general use cases. Feel free to use it as a reference for building your own parser. |
Almost 5 years people are struggling with it and unfortunately no any progress here :( Too sad :( |
This is really an issue for us also. |
Of all the solutions mentioned, I like this solution the best as it doesn't break the original spec |
I moved to <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency> With this dependency I'm able to use a json path like
|
I have a simple workaround. The idea is to make the jsonPath multiple pieces and run
For example, |
just a suggestion on the jsonpath syntax: |
I've just come across this issue as well. I'd assumed that JSONPath was the JSON version of XPath for XML documents. The not being able to index the result of a filter is quite a pain. This ticket has been open for 7 years now. Any prospect of it happening? |
I encountered exactly the same issue as discussed on this page. Because the order in my response array under test is pretty complex, I want to test the individual entries for the correct values baased on the unique "token" field of each response array element. Result after filtering:
And in the unit test: And this works for now. |
It would not help to paginate the array. Custom functions would make a bad palliative, there's no reason why arrays from filter expressions should behave differently from other arrays. |
did you find any workaround? |
This issue is still open. 8 years, incredible! |
eg. I want get the price for book "Sayings of the Century"
👍
$.store.book[?(@.title=='Sayings of the Century')]
will return an book array👍
$.store.book[?(@.title=='Sayings of the Century')].price
will return an price array😂
$.store.book[?(@.title=='Sayings of the Century')][0]
will return an empty array😂
$.store.book[?(@.title=='Sayings of the Century')].price[0]
will return an empty arrayI think
$.store.book[?(@.title=='Sayings of the Century')][0]
should return a book$.store.book[?(@.title=='Sayings of the Century')].price[0]
should return a priceThe text was updated successfully, but these errors were encountered: