json-path/JsonPath · error · MappingException

can not map a " + src.getClass() + " to " + Float.class.getN

Error message

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

What it means

JsonSmartMappingProvider's FloatReader.convert() cannot convert the source object to a java.lang.Float. It accepts Float, Double, Integer, Long, Short, Byte, BigDecimal and String sources; anything else (JSONObject, JSONArray, Boolean, Date, custom types) throws MappingException.

Source

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

        }
        public Float convert(Object src) {
            if(src == null){
                return null;
            }
            if(Float.class.isAssignableFrom(src.getClass())){
                return (Float) src;
            } else if (Integer.class.isAssignableFrom(src.getClass())) {
                return ((Integer) src).floatValue();
            } else if (Long.class.isAssignableFrom(src.getClass())) {
                return ((Long) src).floatValue();
            } else if (BigDecimal.class.isAssignableFrom(src.getClass())) {
                return ((BigDecimal) src).floatValue();
            } else if (Double.class.isAssignableFrom(src.getClass())) {
                return ((Double) src).floatValue();
            } else if (String.class.isAssignableFrom(src.getClass())) {
                return Float.valueOf(src.toString());
            }
            throw new MappingException("can not map a " + src.getClass() + " to " + Float.class.getName());
        }
    }
    private static class BigDecimalReader extends JsonReaderI<BigDecimal> {
        public BigDecimalReader() {
            super(null);
        }
        public BigDecimal convert(Object src) {
            if(src == null){
                return null;
            }
            return new BigDecimal(src.toString());
        }
    }
    private static class BigIntegerReader extends JsonReaderI<BigInteger> {
        public BigIntegerReader() {
            super(null);
        }
        public BigInteger convert(Object src) {

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Ensure the source is a number or a well-formed numeric String before mapping to Float.class.
  2. Adjust the JsonPath to select the scalar float field, not a node.
  3. Read as Object and convert manually with instanceof Number / Float.parseFloat.
  4. Use Jackson or Gson based MappingProvider for wider type support.
  5. Implement a custom MappingProvider if you must map container/bean types.

Example fix

// before
Float f = cfg.mappingProvider().map(booleanValue, Float.class, cfg); // throws
// after
Object v = JsonPath.read(json, "$.ratio");
Float f = (v instanceof Number) ? ((Number) v).floatValue() : Float.parseFloat(v.toString());
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = JsonPath.read(json, "$.field");
if (!(v instanceof Number || v instanceof String))
    throw new IllegalArgumentException("Cannot map " + (v == null ? "null" : v.getClass()) + " to Float");

Type guard

static Float toFloat(Object v) {
    if (v instanceof Number) return ((Number) v).floatValue();
    if (v instanceof String) return Float.valueOf((String) v);
    return null;
}

Try / catch

try {
    Float f = cfg.mappingProvider().map(value, Float.class, cfg);
} catch (MappingException e) {
    Float f = toFloat(value);
    if (f == null) throw new IllegalArgumentException("Non-numeric value", e);
}

Prevention

When it happens

Trigger: map(obj, Float.class, configuration) via JsonSmartMappingProvider where obj's runtime type is not a number or numeric String — commonly a container node or a boolean.

Common situations: Mapping an array/object node to Float because the JsonPath matched the wrong element; expecting String-to-Float parsing that fails on locale-formatted numbers (throws NumberFormatException from Float.valueOf instead); provider differences across environments.

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