json-path/JsonPath · error · UnsupportedOperationException

can not be converted to JSON

Error message

 can not be converted to JSON

What it means

JsonSmartJsonProvider.toJson() serializes objects it knows about (Map via fastjson JSONObject.toJSONString, List, Number, Boolean via JSONValue). Any other type — a POJO, Date, nested custom object — falls into the else branch and throws UnsupportedOperationException naming the class. net.sf.json-smart's provider cannot serialize arbitrary Java objects to JSON.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonSmartJsonProvider.java:89

            return createParser().parse(new InputStreamReader(jsonStream, charset), mapper);
        } catch (ParseException e) {
            throw new InvalidJsonException(e);
        } catch (UnsupportedEncodingException e) {
            throw new JsonPathException(e);
        }
    }

    @Override
    public String toJson(Object obj) {

        if (obj instanceof Map) {
            return JSONObject.toJSONString((Map<String, ?>) obj, JSONStyle.LT_COMPRESS);
        } else if (obj instanceof List) {
            return JSONArray.toJSONString((List<?>) obj, JSONStyle.LT_COMPRESS);
        } else if (obj instanceof Number ||  obj instanceof Boolean){
            return JSONValue.toJSONString(obj);
        } else {
            throw new UnsupportedOperationException(obj.getClass().getName() + " can not be converted to JSON");
        }
    }

    private JSONParser createParser() {
        return new JSONParser(parseMode);
    }
}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Convert the object to a Map/List of JSON-smart-compatible types before calling toJson
  2. Manually serialize with the object's own library (e.g. fastjson JSON.toJSONString(pojo)) and parse the result into a Map first
  3. Use a different JsonProvider (Jackson/Gson based) that supports POJO serialization if toJson of arbitrary objects is required
  4. If it's a bean, add a toMap()/toJson-friendly representation in the domain class

Example fix

// before
String json = Configuration.defaultConfiguration().jsonProvider().toJson(myPojo);
// after
String json = JSON.toJSONString(myPojo); // fastjson handles arbitrary beans
Defensive patterns

Strategy: type-guard

Validate before calling

Object o = ...;
boolean jsonSmartSerializable = o instanceof Map || o instanceof List || o instanceof Number || o instanceof Boolean;
if (!jsonSmartSerializable) o = convertToMapOrList(o);

Type guard

static boolean toJsonCapable(Object o) {
    return o instanceof Map || o instanceof List || o instanceof Number || o instanceof Boolean;
}

Try / catch

try {
    String json = provider.toJson(obj);
} catch (UnsupportedOperationException e) {
    String json = JSON.toJSONString(obj); // fall back to fastjson direct serialization
}

Prevention

When it happens

Trigger: Calling JsonPath.toJson(obj) (or otherwise invoking JsonSmartJsonProvider.toJson) with an object that is not a Map, List, Number, or Boolean — e.g. a Java bean, POJO, or org.json JSONObject from a different provider.

Common situations: Trying to serialize mapped results of a custom MappingProvider (POJOs) back to JSON; mixing JSON libraries so the object at hand is from another library (org.json, Gson, Jackson types); serializing Dates or nested domain objects.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/4209a3430220e92c. Report an issue: GitHub.