grpc/grpc-java · error · ClassCastException

value for idx in is not object

Error message

value %s for idx %d in %s is not object

What it means

checkObjectList validates that every element of a JSON list is a Map (a JSON object) and returns the list cast to List<Map<String, ?>>. Any non-object element triggers ClassCastException 'value ... is not object' with the index and full list. Used for list-of-object config fields like retry/ hedging policy lists.

Solutions

  1. Wrap each element of the list in an object: [1,2] -> [{...}] per the gRPC service config schema.
  2. Look at the printed index in the message to find exactly which element is wrong.
  3. Validate the list-of-objects shape with JSON Schema before parsing.

Example fix

// before
{"methodConfig": [3, 5]}
// after
{"methodConfig": [{"name": [{}]}]}
Defensive patterns

Strategy: validation

Validate before calling

Object v = config.get("methodConfig");
if (v instanceof List) {
  for (Object e : (List<?>) v) {
    if (!(e instanceof Map)) {
      throw new IllegalArgumentException("methodConfig elements must be objects, got: " + e);
    }
  }
}

Type guard

boolean isListOfObjects(Object v) {
  return v instanceof List && ((List<?>) v).stream().allMatch(e -> e instanceof Map);
}

Try / catch

try {
  List<Map<String, ?>> l = JsonUtil.checkObjectList(rawList);
} catch (ClassCastException e) {
  log.error("List-of-objects config field malformed: " + e.getMessage());
}

Prevention

When it happens

Trigger: JsonUtil.checkObjectList(rawList) — reached via getListOfObjects — where the list contains a scalar or array element, e.g. "retryPolicy": [1,2] instead of [{...}].

Common situations: Service config where an array of objects was flattened into an array of scalars; a mix like [{...}, "oops"]; upstream API returning a differently shaped list than the config schema expects.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/a076be31cf0908e2. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/io/grpc/internal/JsonUtil.java:247

      return null;
    }
    Object value = obj.get(key);
    if (!(value instanceof Boolean)) {
      throw new ClassCastException(
          String.format("value '%s' for key '%s' in '%s' is not Boolean", value, key, obj));
    }
    return (Boolean) value;
  }

  /**
   * Casts a list of unchecked JSON values to a list of checked objects in Java type.
   * If the given list contains a value that is not a Map, throws an exception.
   */
  @SuppressWarnings("unchecked")
  public static List<Map<String, ?>> checkObjectList(List<?> rawList) {
    for (int i = 0; i < rawList.size(); i++) {
      if (!(rawList.get(i) instanceof Map)) {
        throw new ClassCastException(
            String.format(
                Locale.US, "value %s for idx %d in %s is not object", rawList.get(i), i, rawList));
      }
    }
    return (List<Map<String, ?>>) rawList;
  }

  /**
   * Casts a list of unchecked JSON values to a list of String. If the given list
   * contains a value that is not a String, throws an exception.
   */
  @SuppressWarnings("unchecked")
  public static List<String> checkStringList(List<?> rawList) {
    for (int i = 0; i < rawList.size(); i++) {
      if (!(rawList.get(i) instanceof String)) {
        throw new ClassCastException(
            String.format(
                Locale.US,

View on GitHub (pinned to 64daddc1f3)