JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
963 stars 169 forks source link

@parent does not actually point to parent when used in a filter on an object #135

Open iwb-rubentytgat opened 3 years ago

iwb-rubentytgat commented 3 years ago

Describe the bug

When running a filter on an object instead of an array, the @parent property does not seem to point at the actual parent object, but at the parent of the parent.

It's easy to imagine where this comes from, since when using this filter on an array of objects, you'd have to go 2 levels up in order to get to the parent object instead of the array. This is however not the case when filtering over the entries of an object.

Code sample or steps to reproduce

I ran the following JSONPath in the browser demo. As you can see I put a console.log statement in the filter to monitor the values of @, @path, @parentProperty and @parent while it is running

$.foo.bar[?(@property === 'foo' && (console.log(@ , @path, @parentProperty, @parent), true))]

with the following JSON:

{
  "id": 1,
  "foo": {
    "id": 2,
    "bar": {
      "id": 3,
      "foo": {
        "id": 4
      }
    }
  }
}

Console error or logs

The console will show the following log:

Object { id: 4 }      $['foo']['bar']['foo']      bar      Object { id: 2, bar: {…} }

i.e. the @parent is not pointing to the object with id 3 as expected, but to it's parent.

Note that the @path and @parentProperty variables are set correctly. Only the @parent property is wrong.

Expected behavior

I would expect the following to be logged instead:

Object { id: 4 }      $['foo']['bar']['foo']      bar      Object { id: 3, foo: {…} }

Environment (IMPORTANT)

Desktop**