{"record":{"id":"de59914606434937","repo":"eclipse-vertx/vert.x","slug":"invalid-json-object-buf","errorCode":null,"errorMessage":"Invalid JSON object: ${buf}","messagePattern":"Invalid JSON object: (.+?)","errorType":"exception","errorClass":"DecodeException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/json/JsonObject.java","lineNumber":88,"sourceCode":"  public JsonObject(Map<String, Object> map) {\n    if (map == null) {\n      throw new NullPointerException();\n    }\n    this.map = map;\n  }\n\n  /**\n   * Create an instance from a buffer.\n   *\n   * @param buf the buffer to create the instance from.\n   */\n  public JsonObject(Buffer buf) {\n    if (buf == null) {\n      throw new NullPointerException();\n    }\n    fromBuffer(buf);\n    if (map == null) {\n      throw new DecodeException(\"Invalid JSON object: \" + buf);\n    }\n  }\n\n  /**\n   * Create a JsonObject containing zero mappings.\n   *\n   * @return an empty JsonObject.\n   */\n  public static JsonObject of() {\n    return new JsonObject();\n  }\n\n  /**\n   * Create a JsonObject containing a single mapping.\n   *\n   * @param k1 the mapping's key\n   * @param v1 the mapping's value\n   * @return a JsonObject containing the specified mapping.","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/json/JsonObject.java#L70-L106","documentation":"JsonObject(Buffer) throws DecodeException when the buffer's bytes cannot be decoded into a JSON object (e.g. they contain a JSON array, string, or invalid data). The null-buffer case is a separate NullPointerException. Vert.x requires the buffer to hold a valid JSON object value.","triggerScenarios":"new JsonObject(Buffer) called with a buffer containing: an empty/blank payload, a JSON array like '[1,2]', a bare scalar like '\"text\"' or '42', trailing garbage, or truncated/invalid JSON.","commonSituations":"Reading a config file that is actually a JSON array or is empty; consuming an HTTP response body that is a JSON array instead of an object; a producer wrote newline-delimited JSON and a consumer tries to parse the whole stream as one object.","solutions":["Inspect/print the buffer content (buf.toString()) to see what is actually being parsed.","If the payload is a JSON array, parse it as JsonArray: new JsonArray(buf).","Validate the bytes are well-formed JSON object text before constructing (e.g. Json.decodeValue first).","Fix the upstream producer or config source to emit a single JSON object."],"exampleFix":"// before\nJsonObject obj = new JsonObject(buffer); // DecodeException if buffer holds an array\n// after\nObject decoded = Json.decodeValue(buffer);\nJsonObject obj = decoded instanceof JsonObject\n  ? (JsonObject) decoded\n  : new JsonObject().put(\"items\", decoded);","handlingStrategy":"validation","validationCode":"Object decoded = Json.decodeValue(buf); // throws early with a clearer message\nif (!(decoded instanceof JsonObject)) throw new IllegalArgumentException(\"Expected JSON object, got \" + decoded.getClass().getSimpleName());","typeGuard":"boolean isJsonObject(Buffer buf) {\n  try { return Json.decodeValue(buf) instanceof JsonObject; } catch (DecodeException e) { return false; }\n}","tryCatchPattern":"try {\n  JsonObject obj = new JsonObject(buf);\n} catch (DecodeException e) {\n  logger.error(\"Not a JSON object: {}\", buf, e);\n  // fall back to defaults or JsonArray handling\n}","preventionTips":["Always decode with Json.decodeValue and branch on the result type before constructing JsonObject.","Never assume an HTTP body or config file is an object; check the first byte is '{'.","Log the raw buffer on failure to diagnose upstream producers."],"tags":["json","decode","buffer"],"backgroundTag":"json-decode-failed","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}