json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.9k stars 1.64k forks source link

JSON Path is taking duplicate values #559

Open KandasamySurya opened 5 years ago

KandasamySurya commented 5 years ago

Hi Team,

Here is the json am using

[{ "temperature": 54.4461, "area": "US", "listeners": [{ "area": 1, "areaCode": 0, "places": 780 }], "province": "california" }, { "temperature": 59.4461, "area": "US", "listeners": [{ "area": 1, "areaCode": 3, "places": 780 }], "province": "ohaiio" }, { "temperature": 51.4461, "area": "US", "listeners": [{ "area": 1, "areaCode": 0, "places": 780 }], "province": "florida" }, { "temperature": 54.4461, "area": "US", "listeners": [{ "area": 1, "areaCode": 0, "places": 780 }], "province": "california" } ]

I have a json like this and i am trying to extract temperature of the places where area code is 0

I have used the below path to extract the json values.

$[?(@.listeners.[?(@.areaCode == 0)])].temperature

but its taking all the values

hf-kklein commented 5 years ago

I'd say this is a bug (similar to #287 and #466 where you can't filter on properties of objects inside an array).

A limited workaround for those objects where the size of the array is 1 like in your example may be this path: $[?(@.listeners[0].areaCode==0)].temperature

If you do control the actual Java code, not only the applied JsonPath, you could get a list of normalized paths using the Option.AS_PATH_LIST option with this path: $[?(@.listeners)][*][?(@.areaCode==0)]:

[
   "$[0]['listeners'][0]",
   "$[2]['listeners'][0]",
   "$[3]['listeners'][0]"
] 

and read the temperature for each listener afterwards. But it's not an elegant solution at all.