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

Proposal - Selecting a subset of an object's properties #49

Open
gregsdennis opened this issue Jan 5, 2021 · 7 comments
Open

Proposal - Selecting a subset of an object's properties #49

gregsdennis opened this issue Jan 5, 2021 · 7 comments
Labels

Comments

@gregsdennis
Copy link
Collaborator

There's no syntax for selecting multiple individual properties from a single object. You either have to select a single property or the entire object.

I would like to propose a "union" or "subselection" syntax (x,y,...) where x, etc. are the properties to be returned. The return value will be an object omitting all properties that are not included in the union declaration.

Using Goessner's example, I could use the path $.store.book[*].(title,price) to select only the title and price of all of the books. It would return

[
  { "title": "Sayings of the Century", "price": 8.95 },
  { "title": "Sword of Honour", "price": 12.99 },
  { "title": "Moby Dick", "price": 8.99 },
  { "title": "The Lord of the Rings", "price": 22.99 },
]

*NOTE I understand the name conflicts with current usage of "union", but that be resolved by changing that usage. I'm struggling to come up with another name.

@gregsdennis gregsdennis changed the title Proposal - Selecting multiple properties Proposal - Selecting a subset of an object's properties Jan 5, 2021
@cburgmer
Copy link

cburgmer commented Jan 5, 2021

I like to thing of there being at least two different problems many people try solving in this space: That of selecting values, and that of transforming the original JSON structure. I understand JSONPath to solve the former, but to be really bad at the latter (by choice).
Selecting a subset for me is clearly a transformation problem, as I can already use $["a", "b"] to select the necessary values.
So my personal stance is to not get into that.

@gregsdennis
Copy link
Collaborator Author

@cburgmer what's the support for something like $["a","b"]? I'm not sure I've seen that anywhere.

@mkmik
Copy link
Collaborator

mkmik commented Jan 5, 2021

fwiw I'm also in the camp of "jsonpath selects and doesn't transform"

@cburgmer
Copy link

cburgmer commented Jan 5, 2021

@cburgmer what's the support for something like $["a","b"]? I'm not sure I've seen that anywhere.

I'm talking about the union operator with two keys: https://cburgmer.github.io/json-path-comparison/results/union_with_keys.html. You will get the values, but not the keys.

Java's com.jayway.jsonpath interestingly enough decides to return the object with the keys, contrary to the rest of the implementations (and Objective-C's SMJJSONPath which according to it's goal just follows com.jayway.jsonpath verbatim).

@cburgmer
Copy link

cburgmer commented Jan 5, 2021

For further reference, Ruby's jsonpath has a "parens notation" (https://cburgmer.github.io/json-path-comparison/results/parens_notation.html) to select a subset: $(key,more). Other implementations might have found their own syntax for supporting something like that, this is the only one I came across though.

@cabo cabo added the enhancement New feature or request label Mar 3, 2021
@goessner
Copy link
Collaborator

goessner commented Mar 8, 2021

Interesting proposal regarding the syntax.

I am also in the camp of "jsonpath selects and doesn't transform" (I've even been the first one :)

The example above could be rewritten to $.store.book[?(@.title && @.price)], which returns a list of output pathes

[ "$['store']['book'][0]",
  "$['store']['book'][1]",
  "$['store']['book'][2]",
  "$['store']['book'][3]"
]

that can be used then to do any post-processing ... here transform to property-reduced books.

No violation of the minimalism priciple this way!

Proposed syntax (<expr1>,<expr2>,...) might be used to separate a brackets child selector from that, what is still called 'union' at current.

@cabo
Copy link
Member

cabo commented Jan 17, 2022

See resolution of #109 at IETF 112.
As we don't do projection in -base, this is now closed/revisit-after-base-done.

@cabo cabo closed this as completed Jan 17, 2022
@glyn glyn reopened this Dec 20, 2023
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

6 participants