json-path/JsonPath · error · IllegalStateException

Target json value must a number, not " + state

Error message

Target json value must a number, not " + state

What it means

isIntegralNumber() in the JakartaMappingProvider JSON-P adapter only works when the parser's current event is VALUE_NUMBER; otherwise it throws this IllegalStateException. It is a strict precondition check: the method exists so callers can test whether the current number is integral, so calling it when no number is current is a programming error.

Source

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

        public String getString() {
            switch (state) {
            case KEY_NAME:
                return ((JsonObjectScope) scope).getKey();
            case VALUE_STRING:
                return ((JsonString) scope.getValue()).getString();
            case VALUE_NUMBER:
                return ((JsonNumber) scope.getValue()).toString();
            default:
                throw new IllegalStateException("Parser is not in KEY_NAME, VALUE_STRING, or VALUE_NUMBER state");
            }
        }

        @Override
        public boolean isIntegralNumber() {
            if (state == Event.VALUE_NUMBER) {
                return ((JsonNumber) scope.getValue()).isIntegral();
            }
            throw new IllegalStateException("Target json value must a number, not " + state);
        }

        @Override
        public int getInt() {
            if (state == Event.VALUE_NUMBER) {
                return ((JsonNumber) scope.getValue()).intValue();
            }
            throw new IllegalStateException("Target json value must a number, not " + state);
        }

        @Override
        public long getLong() {
            if (state == Event.VALUE_NUMBER) {
                return ((JsonNumber) scope.getValue()).longValue();
            }
            throw new IllegalStateException("Target json value must a number, not " + state);
        }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Guard with the current Event: only call isIntegralNumber() after next() returned Event.VALUE_NUMBER
  2. Switch on the event and route each token type to its proper accessor before probing numeric traits
  3. If you need to test the type of arbitrary values, inspect the JsonValue value type instead of the parser-state accessors

Example fix

// before
boolean integral = parser.isIntegralNumber(); // throws unless on a number
// after
if (parser.next() == Event.VALUE_NUMBER) {
    boolean integral = parser.isIntegralNumber();
}
Defensive patterns

Strategy: type-guard

Validate before calling

// guard before probing numeric traits
if (state == Event.VALUE_NUMBER) { parser.isIntegralNumber(); }

Type guard

boolean isNumberToken(Event ev) {
    return ev == Event.VALUE_NUMBER;
}

Try / catch

try {
    boolean integral = parser.isIntegralNumber();
} catch (IllegalStateException e) {
    // current token is not a number; handle non-numeric branch
}

Prevention

When it happens

Trigger: Calling isIntegralNumber() while the parser state is not Event.VALUE_NUMBER — e.g. current scope is a string, array, object, boolean, or null value.

Common situations: Generic number-handling code that forgets to check the current event before probing numeric properties; iterating mixed-type JSON and applying numeric APIs to non-numeric tokens.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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