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
- Wrap each element of the list in an object: [1,2] -> [{...}] per the gRPC service config schema.
- Look at the printed index in the message to find exactly which element is wrong.
- 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
- Follow the gRPC service config schema for list fields exactly.
- Schema-validate arrays-of-objects before parsing.
- Check upstream API responses against expected shapes.
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
- value ' ' for idx in ' ' is not string
- value ' ' for key ' ' in ' ' is not Boolean
- value ' ' for key ' ' in ' ' is not String
- Authorization policy should be a JSON object. Found: null
- Number expected to be integer:
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)