json-path/JsonPath · error · IllegalArgumentException

Cannot determine length of

Error message

Cannot determine length of 

What it means

TapestryJsonProvider.length() supports only org.apache.tapestry5.json.JSONArray and JSONObject; anything else triggers IllegalArgumentException "Cannot determine length of <obj>, unsupported type." Like the other providers, it implements the JsonPath length function contract but only for Tapestry's JSON container types.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java:88

  }

  @Override
  public Object getMapValue(final Object obj, final String key) {
    JSONObject json = (JSONObject) obj;
    if (!json.has(key)) {
      return UNDEFINED;
    }
    return json.get(key);
  }

  @Override
  public int length(final Object obj) {
    if (obj instanceof JSONArray) {
      return ((JSONArray) obj).length();
    } else if (obj instanceof JSONObject) {
      return ((JSONObject) obj).length();
    } else {
      throw new IllegalArgumentException("Cannot determine length of " + obj + ", unsupported type.");
    }
  }

  @Override
  public boolean isArray(final Object obj) {
    return (obj instanceof JSONArray);
  }

  @Override
  public void setArrayIndex(final Object array, final int index, final Object newValue) {
    Object v = newValue == null ? JSONObject.NULL : newValue;
    JSONArray list = (JSONArray) array;
    list.put(index, v);
  }

}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Check that the path targets an array or object before using length()
  2. Adjust the path or add array indices so the target is a JSONArray
  3. Handle scalars separately: read the node and compute length only when it is an array/object
  4. Use SUPPRESS_EXCEPTIONS or a default value in Configuration for nullable paths

Example fix

// before
int n = JsonPath.parse(doc).read("$.count.length()"); // count is a number
// after
Integer count = JsonPath.parse(doc).read("$.count"); // plain scalar read, no length()
Defensive patterns

Strategy: type-guard

Validate before calling

Object node = doc.read(path);
if (!(node instanceof JSONArray) && !(node instanceof JSONObject)) {
    throw new IllegalArgumentException("length() requires array/object, got: " + node);
}

Type guard

static boolean lengthSupported(Object o) {
    return o instanceof JSONArray || o instanceof JSONObject;
}

Try / catch

try {
    int n = doc.read("$.items.length()");
} catch (IllegalArgumentException e) {
    // node is scalar or null — handle accordingly
    n = -1;
}

Prevention

When it happens

Trigger: Evaluating a JsonPath with a length() function where the selected node is a Tapestry5 JSON scalar (String, Number, Boolean, JSONObject.NULL) or an object of a foreign JSON library rather than JSONArray/JSONObject.

Common situations: Applying length() to scalar values (numbers/booleans/null) in the document; using a document parsed by a different library with the Tapestry provider; paths resolving to JSONObject.NULL.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/535e63a175df51b9. Report an issue: GitHub.