json-path/JsonPath · error · MappingException

can not map a " + src.getClass() + " to " + Double.class.get

Error message

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

What it means

JsonSmartMappingProvider's DoubleReader.convert() cannot convert the source object to a java.lang.Double. It handles Double, Float, Integer, Long, Short, Byte, BigDecimal and String sources; any other runtime type reaches the final throw. Containers like JSONObject/JSONArray are never coerced to a primitive wrapper.

Source

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

        }
        public Double convert(Object src) {
            if(src == null){
                return null;
            }
            if(Double.class.isAssignableFrom(src.getClass())){
                return (Double) src;
            } else if (Integer.class.isAssignableFrom(src.getClass())) {
                return ((Integer) src).doubleValue();
            } else if (Long.class.isAssignableFrom(src.getClass())) {
                return ((Long) src).doubleValue();
            } else if (BigDecimal.class.isAssignableFrom(src.getClass())) {
                return ((BigDecimal) src).doubleValue();
            } else if (Float.class.isAssignableFrom(src.getClass())) {
                return ((Float) src).doubleValue();
            } else if (String.class.isAssignableFrom(src.getClass())) {
                return Double.valueOf(src.toString());
            }
            throw new MappingException("can not map a " + src.getClass() + " to " + Double.class.getName());
        }
    }
    private static class FloatReader extends JsonReaderI<Float> {
        public FloatReader() {
            super(null);
        }
        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();

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Verify the resolved value is numeric or a parseable numeric String before mapping to Double.class.
  2. Fix the JsonPath expression to select the scalar numeric field.
  3. Convert manually after reading as Object (instanceof Number -> doubleValue, else Double.parseDouble).
  4. Switch to JacksonMappingProvider/GsonMappingProvider for broader coercion.
  5. Provide a custom MappingProvider for exotic source types.

Example fix

// before
Double d = cfg.mappingProvider().map(jsonArray, Double.class, cfg); // throws
// after
Object v = JsonPath.read(json, "$.price");
Double d = Double.valueOf(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 Double");

Type guard

static Double toDouble(Object v) {
    if (v instanceof Number) return ((Number) v).doubleValue();
    if (v instanceof String) return Double.valueOf((String) v);
    return null;
}

Try / catch

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

Prevention

When it happens

Trigger: map(obj, Double.class, configuration) with JsonSmartMappingProvider where obj is e.g. a JSONObject, JSONArray, Boolean, Date or custom object instead of a number or numeric String.

Common situations: Pointing a path at an object/array instead of a numeric leaf; mapping boolean flags to 0.0/1.0 expectations from another library; swapping mapping providers between environments (JsonSmart in prod, Jackson in tests) so one side throws.

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