wanglingsong / JsonSurfer

A streaming JsonPath processor in Java
MIT License
294 stars 55 forks source link

Filter does not apply to object fields when wildcard used #64

Open ghost opened 4 years ago

ghost commented 4 years ago

Steps to reproduce

Example JSON:

{
    "a": {
        "size": 15
    },
    "b": {
        "size": 20
    },
    "c": {
        "size": 10
    }
}

Queries:

$[?(@.size > 13)]
$.*[?(@.size > 13)]

Code:

String json = "...";  // see JSON file above
Collector collector = JsonSurferJackson.INSTANCE.collector(json);
ValueBox<Collection<Object>> first = collector.collectAll("$[?(@.size > 10)]");
ValueBox<Collection<Object>> second = collector.collectAll("$.*[?(@.size > 10)]");
collector.exec();

System.out.println(first.get());
System.out.println(second.get());

Expected

The queries $.*[?(@.size > 13)] and $[?(@.size > 13)] return an array of two items:

[ { "size": 15 }, { "size": 20 } ]

Actual

Both queries return an empty array:

[]

Note

The queries $.* and $[*] return an array of all three items:

[
    "a": {
        "size": 15
    },
    "b": {
        "size": 20
    },
    "c": {
        "size": 10
    }
]

So I would expect that a filter after a wildcard is working fine.

Environment

JsonSurfer 1.6.0 + Jackson Parser + Jackson Provider.

wanglingsong commented 4 years ago

Currently, in JsonSurfer the filter is supported for array structure only. There is not an official standard for JsonPath syntax for now. You can check the differences among JsonPath projects in this repo: https://github.com/cburgmer/json-path-comparison So I'm not sure your expected result is "real" expected. However, if there are a huge demand on your use-case, I can implement it.

wolfgangcolsman commented 3 years ago

We ran into the same issue. It would be awesome if this can be supported.

A second very similar case we encountered: to objects of the same name (instead of using an array). Seems to be a valid json too (at least jackson can process it).

"a": { "size": 15 }, "a": { "size": 20 }

instead of:

"a": [ { "size": 15 }, { "size": 20 } ]

See also https://github.com/cburgmer/json-path-comparison/issues/41