{"record":{"id":"32192cf0a7ee0f51","repo":"apache/hadoop","slug":"failed-to-parse-json","errorCode":null,"errorMessage":"Failed to parse JSON","messagePattern":"Failed to parse JSON","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java","lineNumber":58,"sourceCode":"  private static final ObjectMapper MAPPER = new ObjectMapper();\n\n  private JsonUtils() {\n  }\n\n  /**\n   * Parse a JSON string into a Java object of the given type.\n   * This method replaces {@code org.eclipse.jetty.util.ajax.JSON.parse}\n   * which did not throw checked exceptions.\n   * @param json the JSON string\n   * @param clazz the target class to deserialize into\n   * @param <T> the type of the parsed object\n   * @return the parsed object\n   */\n  public static <T> T parse(String json, Class<T> clazz) {\n    try {\n      return MAPPER.readValue(json, clazz);\n    } catch (IOException e) {\n      throw new RuntimeException(\"Failed to parse JSON\", e);\n    }\n  }\n\n  /**\n   * Parse a JSON string into a Java object with full generic type info.\n   * Use this overload when the target type has generic parameters,\n   * e.g. {@code new TypeReference<Map<String, Object>>() {}}.\n   * @param json the JSON string\n   * @param typeRef the type reference describing the target type\n   * @param <T> the type of the parsed object\n   * @return the parsed object\n   */\n  public static <T> T parse(String json, TypeReference<T> typeRef) {\n    try {\n      return MAPPER.readValue(json, typeRef);\n    } catch (IOException e) {\n      throw new RuntimeException(\"Failed to parse JSON\", e);\n    }","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java#L40-L76","documentation":"JsonUtils.parse(String, Class) is the checked-exception-free replacement for Jetty's JSON.parse: any Jackson IOException - malformed JSON, wrong shape for the target class, or empty input - is wrapped in RuntimeException(\"Failed to parse JSON\", e). The original Jackson exception is always available via getCause().","triggerScenarios":"JsonUtils.parse(\"{not json\", MyClass.class); or valid JSON whose fields do not match MyClass (cause is MismatchedInputException naming the property); or an empty string (cause is 'No content to map').","commonSituations":"Parsing user-edited JSON config; API responses whose schema changed between versions; strings from logs or message queues that were truncated.","solutions":["Unwrap the cause: JsonParseException names line and column, MismatchedInputException names the offending property.","Validate the payload externally (jq or a JSON linter) and fix the producer.","Align the target class with the real JSON shape; for generic targets use the TypeReference overload."],"exampleFix":"// before\nMyDto dto = JsonUtils.parse(raw, MyDto.class);\n\n// after\nMyDto dto;\ntry {\n  dto = JsonUtils.parse(raw, MyDto.class);\n} catch (RuntimeException e) {\n  throw new IllegalArgumentException(\"Bad payload from \" + source, e.getCause());\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  T v = JsonUtils.parse(json, clazz);\n} catch (RuntimeException e) {\n  if (e.getCause() instanceof JsonProcessingException) {\n    // Jackson details: line/column or mismatched property in e.getCause().getMessage()\n  }\n  throw e;\n}","preventionTips":["Treat external JSON as untrusted: validate shape before parsing into DTOs.","Log the payload (or its hash plus length) when parse fails so the bad input is recoverable."],"tags":["json","parsing","hadoop-common"],"backgroundTag":"malformed-json","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}