apache/seatunnel · error · RuntimeException

json to map exception!

Error message

json to map exception!

What it means

JsonUtils.toMap wraps any Jackson failure (invalid JSON, JSON that is not an object, or key/value types not matching the requested K/V classes) into this RuntimeException, signaling the json string could not be deserialized into a Map.

Source

Thrown at seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java:229

    /**
     * json to map
     *
     * @param json json
     * @param classK classK
     * @param classV classV
     * @param <K> K
     * @param <V> V
     * @return to map
     */
    public static <K, V> Map<K, V> toMap(String json, Class<K> classK, Class<V> classV) {
        if (StringUtils.isEmpty(json)) {
            return Collections.emptyMap();
        }

        try {
            return OBJECT_MAPPER.readValue(json, new TypeReference<Map<K, V>>() {});
        } catch (Exception e) {
            throw new RuntimeException("json to map exception!", e);
        }
    }

    /**
     * json to object
     *
     * @param json json string
     * @param type type reference
     * @param <T> type
     * @return return parse object
     */
    public static <T> T parseObject(String json, TypeReference<T> type) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }

        try {
            return OBJECT_MAPPER.readValue(json, type);

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Read the chained cause: JsonParseException = invalid JSON; MismatchedInputException = shape/type mismatch
  2. Ensure the input is a JSON object starting with '{'
  3. Adjust generic types so V matches the actual JSON value types (e.g. Map<String,Object> for mixed values)
  4. Validate the JSON string externally (jq / an online parser) before calling

Example fix

// before
Map<String, String> m = JsonUtils.toMap(mixedJson); // values contain numbers/objects
// after
Map<String, Object> m = JsonUtils.toMap(mixedJson);
Defensive patterns

Strategy: validation

Validate before calling

boolean isJsonObject(String s) { return s != null && s.trim().startsWith("{"); }

Try / catch

try {
    Map<String, Object> m = JsonUtils.toMap(json);
} catch (RuntimeException e) {
    throw new IllegalStateException("cannot parse json map: " + e.getCause().getMessage(), e);
}

Prevention

When it happens

Trigger: Calling JsonUtils.toMap(json) with invalid JSON, a JSON array or scalar instead of an object, or values that don't fit the K/V generic types (e.g. numbers when Map<String,String> expected).

Common situations: Parsing config snippets or webhook payloads, reading JSON written by another tool with different types, generic-type erasure surprises when K/V don't match actual JSON types.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/cb3f64a39f84e07d. Report an issue: GitHub.