14

Hi I want get legth of array after Filter with JsonPath.

Is it possible ?

My filter is $.issues.[?(@.severity == 'MAJOR')].length()

My Json is

{
  "issues": [
        {
            "severity": "MAJOR"
        },
                {
            "severity": "MINOR"
        },
                {
            "severity": "MAJOR"
        },
                {
            "severity": "MAJOR"
        },
                {
            "severity": "MAJOR"
        }
]
  
}
Matthis.h
  • 849
  • 3
  • 14
  • 30
  • Can you please tag your question with the language you are working in? See https://stackoverflow.com/help/tagging. In the original JSONPath proposal http://goessner.net/articles/JsonPath/ a query returns *`(array|false)` : Array holding either values or normalized path expressions matching the input path expression, which can be used for lazy evaluation. `false` in case of no match.* So there is nothing that returns the count of matches in the proposal itself. But maybe your language & framework supports something. Or you can just count the number of items returned afterwards. – dbc Apr 23 '18 at 13:58
  • What about Jayway JsonPath implementation: the following JsonPath expression seems to be valid yet it returns incorrect result: [see sample](http://jsonpath.herokuapp.com/?path=$..book[?(@.author%20=~%20/.*REES/i)].length()) – begie Jun 06 '18 at 12:50
  • @begie that example returns the count of keys of the map (category, author, title, price). – Joaquín Fernández Apr 24 '19 at 14:02
  • It's been some time but I believe that's why my comment says incorrect result is returned... – begie Apr 26 '19 at 07:03
  • Any update on this? I think length is just for basic approaches – JRichardsz Jan 22 '21 at 15:35
  • @dbc that's not a valid question, the poster is looking for an answer that is done completely within jsonpath. – Keith Tyler Nov 20 '21 at 00:28

1 Answers1

4

Possible work-around for Jayway JsonPath implementation

$.length($.issues.[?(@.severity == 'MAJOR')].length())

Tested here : http://jsonpath.herokuapp.com/

Akshay G
  • 2,070
  • 1
  • 15
  • 33