{"id":"18378dc7b873a254","repo":"google/gson","slug":"not-a-json-object-this","errorCode":null,"errorMessage":"Not a JSON Object: {this}","messagePattern":"Not a JSON Object: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonElement.java","lineNumber":163,"sourceCode":"   */\n  public boolean isJsonNull() {\n    return this instanceof JsonNull;\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonObject}. If this element is of some\n   * other type, an {@link IllegalStateException} will result. Hence it is best to use this method\n   * after ensuring that this element is of the desired type by calling {@link #isJsonObject()}\n   * first.\n   *\n   * @return this element as a {@link JsonObject}.\n   * @throws IllegalStateException if this element is of another type.\n   */\n  public JsonObject getAsJsonObject() {\n    if (isJsonObject()) {\n      return (JsonObject) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Object: \" + this);\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonArray}. If this element is of some other\n   * type, an {@link IllegalStateException} will result. Hence it is best to use this method after\n   * ensuring that this element is of the desired type by calling {@link #isJsonArray()} first.\n   *\n   * @return this element as a {@link JsonArray}.\n   * @throws IllegalStateException if this element is of another type.\n   */\n  public JsonArray getAsJsonArray() {\n    if (isJsonArray()) {\n      return (JsonArray) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Array: \" + this);\n  }\n\n  /**","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonElement.java#L145-L181","documentation":"Thrown by JsonElement.getAsJsonObject() when the element is not a JsonObject (it is a JsonArray, JsonPrimitive, or JsonNull). It is an unchecked cast that assumes the caller already verified the type.","triggerScenarios":"Calling element.getAsJsonObject() on an element produced by parsing JSON where the value is an array, a primitive, or null instead of an object.","commonSituations":"An API field that is sometimes null or an array; a value that is an object in one environment and null in another; iterating heterogeneous values and calling getAsJsonObject unconditionally; version skew between producer and consumer schemas.","solutions":["Guard with element.isJsonObject() before calling getAsJsonObject().","Handle isJsonNull() explicitly (skip or default) for nullable fields.","If the field can be array-or-object, branch on isJsonArray() first.","For robust parsing, deserialize to a POJO with Gson so shape mismatches surface as structured errors."],"exampleFix":"// before\nJsonObject o = jsonElement.getAsJsonObject(); // throws if not object\n// after\nif (jsonElement.isJsonObject()) {\n  JsonObject o = jsonElement.getAsJsonObject();\n} else if (jsonElement.isJsonNull()) {\n  // handle null\n} else {\n  // unexpected shape\n}","handlingStrategy":"type-guard","validationCode":"// Runtime check before the cast\nif (!element.isJsonObject()) {\n  throw new IllegalStateException(\"expected JSON object, got \" + element.getClass().getSimpleName());\n}\nJsonObject o = element.getAsJsonObject();","typeGuard":"// Narrowing guard\nJsonObject asObjectOrDefault(JsonElement e, JsonObject def) {\n  return e != null && e.isJsonObject() ? e.getAsJsonObject() : def;\n}","tryCatchPattern":"// Catch only at a boundary that must tolerate shape drift\ntry {\n  JsonObject o = el.getAsJsonObject();\n} catch (IllegalStateException e) {\n  // log and use a default/empty object\n}","preventionTips":["Always isJsonObject() before getAsJsonObject().","Branch on isJsonNull() for nullable fields.","Prefer POJO deserialization for stable schemas."],"tags":["gson","json-element","type-mismatch","parsing"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}