37

I'm having a problem with finding an array or list size using Stefan Goessner's JsonPath. I'm using the version json-path-2.0.0.

My jsonpath expression is $.orders.length and JSON looks something like this:

{
  "orders" : [
    ...
  ]
}

Its failing with the following error:

com.jayway.jsonpath.PathNotFoundException: Property ['length'] not found in path $['orders']

And I tried with $.orders.length() too which is again failing with the below error:

com.jayway.jsonpath.PathNotFoundException: Property ['length()'] not found in path $['orders']

Please suggest me how to get the length of the array using Goessner's JsonPath expression.

[EDIT] Following is how I'm obtaining the configuration:

    com.jayway.jsonpath.Configuration conf = com.jayway.jsonpath.Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    DocumentContext documentContext = JsonPath.using(conf).parse(orderJson);
    Object val = documentContext.read(jsonPathExpression);
Nagendra Varma
  • 2,215
  • 3
  • 17
  • 26

5 Answers5

84

It seems that support for returning the length() of an array was only added in version 2.1.0 of the jayway json-path library.

Based on some quick tests, the $.orders.length() expression seems to work with both version 2.1.0 and version 2.2.0, so I think you just need to upgrade your dependency version in order to fix the error you are seeing.

andersschuller
  • 13,509
  • 2
  • 42
  • 33
  • A small correction, seems that it should be $.orders.length (without ()), just tested and works fine for me – Alex Rewa Jan 11 '22 at 15:57
  • @AlexRewa I tried and it doesn't work on my side with the error message ```java.lang.AssertionError: No value at JSON path "$.length"```. But ```"$.length()"``` works fine – Võ Quang Hòa Feb 17 '22 at 06:04
  • @VõQuangHòa, weird... seems it depends on JsonPath implementation. – Alex Rewa Feb 17 '22 at 11:34
3

with webclient in spring webflux

....
 .jsonPath("$.length()").isEqualTo(2);
...
bmck
  • 211
  • 2
  • 6
  • I used your solution in the following way & it worked like a charm! This was to specifically check if an array was empty & has zero objects but is not null. resultActions.andExpect(jsonPath('$.benefitsSummaryResponse.financialData.limitList.length()', is(0)) ) – Aniket Mar 06 '23 at 16:09
0

The way to count number of elements in json array this is how i solve it

myJson ->My Json that inside it exist an array

fieldPath -> the path to the array

import com.jayway.jsonpath.JsonPath;
import org.json.JSONArray;
import org.json.simple.JSONObject;

JSONObject myJson;

    public long CountFields(String fieldPath) {
      String onlyMyArray=JsonPath.parse(myJson).read(fieldPath).toString();
      JSONArray jsonArray = new JSONArray(onlyMyArray);
      return jsonArray.length();
                       }
Vladi
  • 1,662
  • 19
  • 30
-2
            JsonPath js = new JsonPath(response);
           List values = js.getList("Mention the path of the json here");
           int size= values.size();
           System.out.println(size);
-5

The getList() method of JsonPath can be used effectively to convert the array into a list of objects that can then be used with .size()

 // convert each item in the Json array to be an item of the orderItems list
List orderItems = someResponse.jsonPath().getList("orders");

// So now because we have the size of the list, we also have the size of the original array
int orderCount = orderItems.size());
emery
  • 8,603
  • 10
  • 44
  • 51
Shibu
  • 97
  • 1
  • 3
  • 5
    Please always add an explanation to the code. it will improve the quality of the answer – DaFois Jul 13 '19 at 10:59
  • This is the most downvoted useful and correct answer I've ever seen on StackOverflow. the getList() method of JsonPath was extremely useful to me and this is actually the answer I used to solve the problem. I'm cleaning up this answer so that hopefully it can climb back up from -6 LOL – emery Aug 09 '23 at 23:50