json-path/JsonPath · error · MappingException

can not map a " + src.getClass() + " to " + Boolean.class.ge

Error message

can not map a " + src.getClass() + " to " + Boolean.class.getName()

What it means

JsonSmartMappingProvider's BooleanReader.convert() only maps null and actual Boolean instances to Boolean; every other runtime type (String "true"/"false", numbers, containers) throws MappingException. Unlike the numeric readers, there is no String fallback, so stringly-typed booleans are rejected.

Source

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

                } catch (ParseException e) {
                    throw new MappingException(e);
                }
            }
            throw new MappingException("can not map a " + src.getClass() + " to " + Date.class.getName());
        }
    }
    private static class BooleanReader extends JsonReaderI<Boolean> {
        public BooleanReader() {
            super(null);
        }
        public Boolean convert(Object src) {
            if(src == null){
                return null;
            }
            if (Boolean.class.isAssignableFrom(src.getClass())) {
                return (Boolean) src;
            }
            throw new MappingException("can not map a " + src.getClass() + " to " + Boolean.class.getName());
        }
    }
}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Pre-normalize the value: Boolean.parseBoolean(obj.toString()) when it is a String, or obj != 0 for numeric flags.
  2. Make the upstream JSON produce real JSON booleans (true/false without quotes).
  3. Verify the JsonPath selects a boolean leaf, not a string/number field.
  4. Switch to JacksonMappingProvider/GsonMappingProvider, which coerce "true" strings to Boolean.
  5. Register a custom MappingProvider that handles string/number-to-boolean conversion.

Example fix

// before
Boolean b = cfg.mappingProvider().map("true", Boolean.class, cfg); // MappingException
// after
Object v = JsonPath.read(json, "$.enabled");
Boolean b = (v instanceof Boolean) ? (Boolean) v : Boolean.parseBoolean(v.toString());
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = JsonPath.read(json, "$.field");
if (!(v instanceof Boolean))
    throw new IllegalArgumentException("Cannot map " + (v == null ? "null" : v.getClass()) + " to Boolean; JsonSmart provider accepts only real booleans");

Type guard

static Boolean toBool(Object v) {
    if (v instanceof Boolean) return (Boolean) v;
    if (v instanceof String) return Boolean.parseBoolean((String) v);
    if (v instanceof Number) return ((Number) v).intValue() != 0;
    return null;
}

Try / catch

try {
    Boolean b = cfg.mappingProvider().map(value, Boolean.class, cfg);
} catch (MappingException e) {
    Boolean b = toBool(value);
    if (b == null) throw new IllegalArgumentException("Non-boolean value", e);
}

Prevention

When it happens

Trigger: map(obj, Boolean.class, configuration) via JsonSmartMappingProvider where obj is a String (e.g. "true"), an Integer 0/1, a JSONObject, or any non-Boolean type.

Common situations: JSON sources that encode booleans as "true"/"false" strings or 0/1 numbers; data produced by lenient providers (Jackson coerces "true" strings) then read through JsonSmart mapping; paths resolving to the wrong node.

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/fd769047cc1b6581. Report an issue: GitHub.