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

ER - Provide syntax for returning a specific number of filtered elements #95

Open
mkredpoint opened this issue May 11, 2021 · 6 comments
Labels

Comments

@mkredpoint
Copy link

A gentleman by the username of gregsdennis on Stack Overflow suggested I raise this issue here for consideration into the specification:

https://stackoverflow.com/questions/67477431/json-path-is-there-a-way-to-only-return-a-specific-number-of-filtered-elements/67477600

Given an object like this:

{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }

and running a JSON path expression like so:

$..book[?(@.price<10)]

Will return 2 objects:

[ { "category" : "reference", "author" : "Nigel Rees", "title" : "Sayings of the Century", "price" : 8.95 }, { "category" : "fiction", "author" : "Herman Melville", "title" : "Moby Dick", "isbn" : "0-553-21311-3", "price" : 8.99 } ]

it would be great if JSON Path also provide syntax for returning the first or last 'n' items of the filtered array

e.g. if I only wanted maximum 1 item from the result of the filtering operation:

$..book[?(@.price<10)]{1}

@cabo
Copy link
Member

cabo commented Nov 9, 2021

This could also be used in a form of pagination (offset/length), but that could be a local API and not a feature of JSONPath.

@cabo cabo added the enhancement New feature or request label Nov 9, 2021
@gregsdennis
Copy link
Collaborator

I think having a syntax for it is valuable. Individual libraries implementing this independently doesn't serve uniformity.

@glyn
Copy link
Collaborator

glyn commented Nov 10, 2021

I agree such a feature has some value, but I'm not convinced it has enough value to warrant adding complexity to the spec.

@gregsdennis
Copy link
Collaborator

Fair enough. I think this should remain open and perhaps labeled as a v2/future feature.

@cabo
Copy link
Member

cabo commented Jan 17, 2022

112 output:

Consensus: This won't be part of spec as it's an API problem but could be included in future

@cabo cabo closed this as completed Jan 17, 2022
@glyn glyn reopened this Dec 20, 2023
@gregsdennis
Copy link
Collaborator

I actually had this requested recently in json-everything/json-everything#583.

Basically they're doing a recursive descent, but they only want the first result. I ended up making my library return .net's deferred execution Linq queries, which allows them to tack on other .net functionality to make the query stop early rather than continue querying the entire document.

It'd be really nice to have syntax to handle this.

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

4 participants