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
- Pre-normalize the value: Boolean.parseBoolean(obj.toString()) when it is a String, or obj != 0 for numeric flags.
- Make the upstream JSON produce real JSON booleans (true/false without quotes).
- Verify the JsonPath selects a boolean leaf, not a string/number field.
- Switch to JacksonMappingProvider/GsonMappingProvider, which coerce "true" strings to Boolean.
- 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
- Emit real JSON booleans (true/false, unquoted) from upstream sources.
- Never expect "true"/"false" strings or 0/1 numbers to coerce via JsonSmart mapping.
- Read as Object and normalize with a helper before mapping.
- If inputs are stringly-typed, prefer Jackson/Gson providers which coerce strings to Boolean.
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
- can not map a " + src.getClass() + " to " + Integer.class.ge
- can not map a " + src.getClass() + " to " + Long.class.getNa
- can not map a " + src.getClass() + " to " + Double.class.get
- can not map a " + src.getClass() + " to " + Float.class.getN
- can not map a " + src.getClass() + " to " + Date.class.getNa
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/fd769047cc1b6581.
Report an issue: GitHub.