Skip to content

Should filters pushed to the bottom of the query work? #79

Description

@NiklasRosenstein

Hi! I'm not entirely sure if this is supposed to work (though I think it would be intuitive and ergonomic if it would):

data = [
    {"payload": {"type": "A"}},
    {"payload": {"type": "B"}},
    {"payload": {"type": "C"}},
]

print(jsonpath.findall('$[*].payload[?(@.type == "B")]', data))

This gives you an empty list, when instead I would expect it to be equivalent to the result of this (working) version:

print(jsonpath.findall('$[?(@payload.type == "B")].payload', data))

Activity

  1. jg-rp commented on Feb 11, 2025

    @jg-rp
    Owner

    Hi @NiklasRosenstein,

    I believe this behaviour to be correct, according to RFC 9535, albeit not immediately intuitive.

    If we apply $[*].payload to your example data without the filter, we get a node list containing these values:

    [
      {
        "type": "A"
      },
      {
        "type": "B"
      },
      {
        "type": "C"
      }
    ]

    If we then apply the filter, [?(@.type == "B")], to each of those values in turn, it selects nothing, because filters are applied to object/dict values without referencing their keys/properties.

    Changing the filter to [?(@ == "B")] will give us a single node with the value "B".

    import jsonpath
    
    data = [
        {"payload": {"type": "A"}},
        {"payload": {"type": "B"}},
        {"payload": {"type": "C"}},
    ]
    
    print(jsonpath.findall('$[*].payload[?(@ == "B")]', data))  # ['B']
  2. Repository owner locked and limited conversation to collaborators on Aug 3, 2025
  3. converted this issue into a discussion #89 on Aug 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions