Array size using jsonpath expression - Stefan Goessner JsonPath
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.
Related videos on Youtube
Nagendra Varma
A passionate programmer not bound by any technology. However, most of my experience includes working with Java, I can work on any language as I focus on programming constructs rather than language internals.
Updated on December 18, 2021Comments
-
Nagendra Varma over 2 years
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);
-
DaFois almost 5 yearsPlease always add an explanation to the code. it will improve the quality of the answer
-
Alex Rewa over 2 yearsA small correction, seems that it should be $.orders.length (without ()), just tested and works fine for me
-
Võ Quang Hòa over 2 years@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 -
Alex Rewa over 2 years@VõQuangHòa, weird... seems it depends on JsonPath implementation.