json-path-comparison icon indicating copy to clipboard operation
json-path-comparison copied to clipboard

Tests for root reference in filter expressions.

Open gregsdennis opened this issue 3 years ago • 2 comments

I found while answering a StackOverflow question that there aren't any tests checking for root reference in filter expressions:

$.SomeFieldB[?($.SomeFieldA == 'X')].SomeFieldC

for the JSON value

{
    "SomeFieldA": "X",
    "SomeFieldB": {
         "SomeFieldC": "Y"
     }
}

Note how the expression $.SomeFieldA == 'X' refers back to the root, then takes the value of SomeFieldA. This is a syntax which we have decided should be supported and is now in the JSON Path draft.

gregsdennis avatar Jul 25 '21 09:07 gregsdennis

The closest query I can find is https://cburgmer.github.io/json-path-comparison/results/filter_expression_with_equals_with_root_reference.html which however compares the current value with the root value: $.items[?(@.key==$.value)].

Before we add your query, can I suggest to simplify? To me it seems to combine multiple scenarios:

  • Dot notation
  • Filter expression with root reference
  • Filter expression on object

The last scenario isn't even supported uniformly I believe: $.SomeFieldB[?($.SomeFieldA == 'X')] operates on the value "Y", compare https://cburgmer.github.io/json-path-comparison/results/filter_expression_on_object.html.

cburgmer avatar Jul 25 '21 17:07 cburgmer

Sure, it was just the example in the question. Any filter query that references the root is fine.

gregsdennis avatar Jul 25 '21 19:07 gregsdennis

I think the root reference you linked to covers this case. I'm going to close this.

gregsdennis avatar Mar 30 '23 22:03 gregsdennis