json-path/JsonPath · error · InvalidPathException

Path must not end with a '.

Error message

Path must not end with a '.

What it means

readDotToken rejects a path that terminates immediately after a dot, i.e. the path ends with a trailing '.' and no property follows. A dot must be followed by a property name, wildcard, or another token; otherwise compilation fails with InvalidPathException.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java:168

                }
                return true;
            default:
                if (!readPropertyOrFunctionToken(appender)) {
                    fail("Could not parse token starting at position " + path.position());
                }
                return true;
        }
    }

    //
    // . and ..
    //
    private boolean readDotToken(PathTokenAppender appender) {
        if (path.currentCharIs(PERIOD) && path.nextCharIs(PERIOD)) {
            appender.appendPathToken(PathTokenFactory.crateScanToken());
            path.incrementPosition(2);
        } else if (!path.hasMoreCharacters()) {
            throw new InvalidPathException("Path must not end with a '.");
        } else {
            path.incrementPosition(1);
        }
        if(path.currentCharIs(PERIOD)){
            throw new InvalidPathException("Character '.' on position " + path.position() + " is not valid.");
        }
        return readNextToken(appender);
    }

    //
    // fooBar or fooBar()
    //
    private boolean readPropertyOrFunctionToken(PathTokenAppender appender) {
        if (path.currentCharIs(OPEN_SQUARE_BRACKET) || path.currentCharIs(WILDCARD) || path.currentCharIs(PERIOD) || path.currentCharIs(SPACE)) {
            return false;
        }
        int startPosition = path.position();
        int readPosition = startPosition;

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Remove the trailing '.' from the path string.
  2. When building paths programmatically, strip trailing separators: `path.replaceAll("\\.$", "")` or join without a trailing delimiter.
  3. Validate the path (e.g. try JsonPath.compile in a try-catch) before using it on documents.

Example fix

// before
String p = "$.a.b."; // trailing dot
JsonPath.read(json, p);
// after
String p = "$.a.b";
JsonPath.read(json, p);
Defensive patterns

Strategy: validation

Validate before calling

String sanitizePath(String p) {
    return p == null ? null : p.replaceAll("\\.+$", "");
}

Try / catch

try {
    return JsonPath.read(json, path);
} catch (InvalidPathException e) {
    throw new IllegalArgumentException("Malformed path (trailing dot?): " + path, e);
}

Prevention

When it happens

Trigger: Paths like `$.store.`, `$..`, or dynamically built paths where a trailing separator was appended in a loop (`"$." + String.join(".", parts)` with empty parts).

Common situations: Programmatic path assembly with joins producing trailing dots; users typing a path in a UI and leaving a trailing period; copy-paste truncation.

Related errors


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