How to build jsonpath expression for checking the number of times in array?

374 views Asked by At

Suppose I have a json like this { "city" :[ "Bangalore", "Delhi", "Chennai", "Bangalore" ] } Now I want to make a make a json path query to check if bangalore is occuring twice or not so to check that I want to extract no of times Bangalore is coming in array so how can we build the jsonpath expression?

1

There are 1 answers

0
Bartios On

I'm not sure I completely understand what you are asking but I guess you are using jayway's jsonpath library in java. In that case you can probably use this:

int amountOfBangalore = JsonPath.parse(json).read("$.city.[?(@ == \"Bangalore\")]").length();

Where you construct a list filled with each instance of 'bangalore' in the text and then call length() on that.