apache/hadoop · error · RuntimeException

Failed to parse JSON

Error message

Failed to parse JSON

What it means

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().

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java:58

  private static final ObjectMapper MAPPER = new ObjectMapper();

  private JsonUtils() {
  }

  /**
   * Parse a JSON string into a Java object of the given type.
   * This method replaces {@code org.eclipse.jetty.util.ajax.JSON.parse}
   * which did not throw checked exceptions.
   * @param json the JSON string
   * @param clazz the target class to deserialize into
   * @param <T> the type of the parsed object
   * @return the parsed object
   */
  public static <T> T parse(String json, Class<T> clazz) {
    try {
      return MAPPER.readValue(json, clazz);
    } catch (IOException e) {
      throw new RuntimeException("Failed to parse JSON", e);
    }
  }

  /**
   * Parse a JSON string into a Java object with full generic type info.
   * Use this overload when the target type has generic parameters,
   * e.g. {@code new TypeReference<Map<String, Object>>() {}}.
   * @param json the JSON string
   * @param typeRef the type reference describing the target type
   * @param <T> the type of the parsed object
   * @return the parsed object
   */
  public static <T> T parse(String json, TypeReference<T> typeRef) {
    try {
      return MAPPER.readValue(json, typeRef);
    } catch (IOException e) {
      throw new RuntimeException("Failed to parse JSON", e);
    }

View on GitHub (pinned to 2add963021)

Solutions

  1. Unwrap the cause: JsonParseException names line and column, MismatchedInputException names the offending property.
  2. Validate the payload externally (jq or a JSON linter) and fix the producer.
  3. Align the target class with the real JSON shape; for generic targets use the TypeReference overload.

Example fix

// before
MyDto dto = JsonUtils.parse(raw, MyDto.class);

// after
MyDto dto;
try {
  dto = JsonUtils.parse(raw, MyDto.class);
} catch (RuntimeException e) {
  throw new IllegalArgumentException("Bad payload from " + source, e.getCause());
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  T v = JsonUtils.parse(json, clazz);
} catch (RuntimeException e) {
  if (e.getCause() instanceof JsonProcessingException) {
    // Jackson details: line/column or mismatched property in e.getCause().getMessage()
  }
  throw e;
}

Prevention

When it happens

Trigger: 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').

Common situations: Parsing user-edited JSON config; API responses whose schema changed between versions; strings from logs or message queues that were truncated.

Understand the failure class

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/32192cf0a7ee0f51. Report an issue: GitHub.