draft-ietf-jsonpath-base
draft-ietf-jsonpath-base copied to clipboard
ER - Provide syntax for returning a specific number of filtered elements
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}
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.
I think having a syntax for it is valuable. Individual libraries implementing this independently doesn't serve uniformity.
I agree such a feature has some value, but I'm not convinced it has enough value to warrant adding complexity to the spec.
Fair enough. I think this should remain open and perhaps labeled as a v2/future feature.
112 output:
Consensus: This won't be part of spec as it's an API problem but could be included in future
I actually had this requested recently in https://github.com/gregsdennis/json-everything/issues/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.