json-path/JsonPath · error · MappingException

JSON object cannot be databind to " + targetType

Error message

JSON object cannot be databind to " + targetType

What it means

In mapImpl, when the source is a JsonObject and the target type is not Map/JsonObject and JSON-B databinding is unavailable (no Jsonb instance configured, or jsonb provider not on classpath), the provider cannot convert the object and throws MappingException. The JSON-B fallback path is the only way to bind a JsonObject to an arbitrary POJO in this provider.

Source

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

                        JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                        return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);
                    } catch (Exception e){
                        throw new MappingException(e);
                    }
                } else {
                    try {
                        // Fallback databinding approach for JSON-B API implementations without
                        // explicit support for use of JsonParser in their public API. The approach
                        // is essentially first to serialize given value into JSON, and then bind
                        // the JSON string to data object of given type.
                        String json = source.toString();
                        return jsonb.fromJson(json, (Type) targetType);
                    } catch (JsonbException e){
                        throw new MappingException(e);
                    }
                }
            } else {
                throw new MappingException("JSON object cannot be databind to " + targetType);
            }
        } else {
            return source;
        }
    }

    @SuppressWarnings("unchecked")
    private <T> T mapIntegralJsonNumber(JsonNumber jsonNumber, Class<?> targetType) {
        if (targetType.isPrimitive()) {
            if (int.class.equals(targetType)) {
                return (T) Integer.valueOf(jsonNumber.intValueExact());
            } else if (long.class.equals(targetType)) {
                return (T) Long.valueOf(jsonNumber.longValueExact());
            }
        } else if (Integer.class.equals(targetType)) {
            return (T) Integer.valueOf(jsonNumber.intValueExact());
        } else if (Long.class.equals(targetType)) {
            return (T) Long.valueOf(jsonNumber.longValueExact());

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Add a JSON-B implementation (e.g. org.eclipse:yasson) to the classpath so the jsonb.fromJson path works
  2. Map to Map.class or JsonObject.class instead of a POJO and extract fields manually
  3. Register/configure a Jsonb instance with the provider if it supports injection
  4. Switch MappingConfiguration to a provider with built-in databinding (JacksonJsonProvider, GsonJsonProvider)

Example fix

// before
User u = ctx.read("$.user", User.class); // no Jsonb -> MappingException
// after
Map<String, Object> u = ctx.read("$.user", Map.class);
// or add yasson dependency and keep User.class
Defensive patterns

Strategy: fallback

Validate before calling

Configuration.setDefaults(new Configuration.Defaults() {
    public MappingProvider mappingProvider() { return new JakartaMappingProvider(); }
    // ensure a Jsonb-backed path exists or prefer Map.class targets
});
// pre-check: prefer Map/JsonObject for objects when no Jsonb on classpath
Object raw = ctx.read(path);
if (raw instanceof Map) { /* bind manually */ }

Type guard

boolean canDatabind(Class<?> t) { return Map.class.equals(t) || JsonObject.class.equals(t) /* or jsonb available */; }

Try / catch

try {
    User u = ctx.read(path, User.class);
} catch (MappingException e) {
    Map<String, Object> m = ctx.read(path, Map.class);
    // map to POJO manually
}

Prevention

When it happens

Trigger: read(path, MyPojo.class) where the path resolves to a JSON object AND no Jsonb is configured/available in JakartaMappingProvider; or the targetType is a type jsonb.fromJson does not accept (the else branch).

Common situations: Using the Jakarta provider without a JSON-B implementation (e.g. Yasson) on the classpath, so POJO databinding silently isn't available; mapping to a POJO while only Map/JsonObject conversion is supported; expecting default-provider behavior (Jackson/Jayway databinding) after switching MappingConfiguration.

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