json-path/JsonPath · error · MappingException

JSON boolean (true) cannot be mapped to

Error message

JSON boolean (true) cannot be mapped to 

What it means

JakartaMappingProvider.mapImpl maps jakarta.json JsonValue sources to Java target types. When the source is JsonValue.TRUE (or FALSE) it can only return Boolean; if the requested targetType is anything else it throws MappingException naming the target class. JSON booleans cannot legally become e.g. String or Integer, so the mapping is refused.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java:107

     * a parameterized generic type is used.
     */
    @Override
    public <T> T map(Object source, final TypeRef<T> targetType, Configuration configuration) {
        @SuppressWarnings("unchecked")
        T result = (T) mapImpl(source, targetType.getType());
        return result;
    }

    private Object mapImpl(Object source, final Type targetType) {
        if (source == null || source == JsonValue.NULL) {
            return null;
        }
        if (source == JsonValue.TRUE) {
            if (Boolean.class.equals(targetType)) {
                return Boolean.TRUE;
            } else {
                String className = targetType.toString();
                throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
            }
        }
        if (source == JsonValue.FALSE) {
            if (Boolean.class.equals(targetType)) {
                return Boolean.FALSE;
            } else {
                String className = targetType.toString();
                throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
            }
        } else if (source instanceof JsonString) {
            if (String.class.equals(targetType)) {
                return ((JsonString) source).getChars();
            } else {
                String className = targetType.toString();
                throw new MappingException("JSON string cannot be mapped to " + className);
            }
        } else if (source instanceof JsonNumber) {
            JsonNumber jsonNumber = (JsonNumber) source;

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Read the boolean as Boolean.class and convert/represent as needed (String.valueOf) in your code
  2. Validate the document schema so boolean nodes are not requested as other types
  3. Use a mapping target of Object.class when the node type is unknown, then branch on the result
  4. If the path may vary, guard with SUPPRESS_EXCEPTIONS/optional reads and handle type at the call site

Example fix

// before
String s = JsonPath.parse(doc).read("$.enabled", String.class); // throws
// after
Boolean b = JsonPath.parse(doc).read("$.enabled", Boolean.class);
String s = String.valueOf(b); // "true" / "false"
Defensive patterns

Strategy: try-catch

Validate before calling

JsonValue v = doc.read(path);
if (v == JsonValue.TRUE || v == JsonValue.FALSE) {
    // request Boolean.class only
}

Type guard

static boolean isJsonBoolean(jakarta.json.JsonValue v) {
    return v == JsonValue.TRUE || v == JsonValue.FALSE;
}

Try / catch

try {
    T value = JsonPath.parse(doc).read(path, targetType);
} catch (MappingException e) {
    // re-read as Boolean and convert
    Boolean b = JsonPath.parse(doc).read(path, Boolean.class);
}

Prevention

When it happens

Trigger: Calling JsonPath read with a target type like String.class or Integer.class on a path that resolves to a JSON true/false (JsonValue.TRUE/FALSE), e.g. read("$.enabled", String.class) where the document has {"enabled": true}.

Common situations: Assuming booleans stringify to "true"; copying read calls with wrong target classes after a document schema change; generic read-into-map code requesting a fixed type for heterogeneous nodes.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/c763e9d84bddd738. Report an issue: GitHub.