Use the typical json as an example:
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
I want filter the books, and get the filter result array size.
I write the json path as: $..book[?(@.category in ['fiction'])].size()
The expected value is 3. But actually I got 14.
Tried $..book[?(@.category in ['fiction'])].length()
Still got same result: 14
$.store.book[?(@.category=="fiction")].length()
//Output
[
4,
5,
5
]
will give the count of properties in each book. This is the reason you are getting 14 (4+5+5) for $..book[?(@.category == "fiction")].length()
Now, if you use the length function on the resulting array, you will get 3 which is the desired result.
$.length($.store.book[?(@.category == "fiction")].length())
Try here : Jayway JsonPath Evaluator
This is a work-around for your requirement, There are many open issues with similar questions in the github length() not working as expected