Array size using jsonpath expression - Stefan Goessner JsonPath

Nagendra Varma picture Nagendra Varma · May 1, 2016 · Viewed 42.4k times · Source

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:

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);

Answer

andersschuller picture andersschuller · May 1, 2016

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.