draft-ietf-jsonpath-base icon indicating copy to clipboard operation
draft-ietf-jsonpath-base copied to clipboard

Proposal - Selecting a subset of an object's properties

Open gregsdennis opened this issue 4 years ago • 7 comments

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 avatar Jan 05 '21 01:01 gregsdennis

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.

cburgmer avatar Jan 05 '21 10:01 cburgmer

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

gregsdennis avatar Jan 05 '21 12:01 gregsdennis

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

mkmik avatar Jan 05 '21 14:01 mkmik

@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 avatar Jan 05 '21 18:01 cburgmer

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.

cburgmer avatar Jan 05 '21 20:01 cburgmer

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.

goessner avatar Mar 08 '21 17:03 goessner

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

cabo avatar Jan 17 '22 23:01 cabo