{"record":{"id":"c946320a340d2d39","repo":"json-path/JsonPath","slug":"path-must-start-with-or","errorCode":null,"errorMessage":"Path must start with '$' or '@'","messagePattern":"Path must start with '\\$' or '@'","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java","lineNumber":107,"sourceCode":"            char c = path.currentChar();\n            if (!isWhitespace(c)) {\n                break;\n            }\n            path.incrementPosition(1);\n        }\n    }\n\n    private Boolean isPathContext(char c) {\n        return (c == DOC_CONTEXT || c == EVAL_CONTEXT);\n    }\n\n    //[$ | @]\n    private RootPathToken readContextToken() {\n\n        readWhitespace();\n\n        if (!isPathContext(path.currentChar())) {\n            throw new InvalidPathException(\"Path must start with '$' or '@'\");\n        }\n\n        RootPathToken pathToken = PathTokenFactory.createRootPathToken(path.currentChar());\n\n        if (path.currentIsTail()) {\n            return pathToken;\n        }\n\n        path.incrementPosition(1);\n\n        if(path.currentChar() != PERIOD && path.currentChar() != OPEN_SQUARE_BRACKET){\n            fail(\"Illegal character at position \" + path.position() + \" expected '.' or '['\");\n        }\n\n        PathTokenAppender appender = pathToken.getPathTokenAppender();\n        readNextToken(appender);\n\n        return pathToken;","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java#L89-L125","documentation":"The PathCompiler's readContextToken requires the very first token of a path to be '$' (root) or '@' (current/filter context). Any other starting character makes the path unparseable, so InvalidPathException is thrown during compilation.","triggerScenarios":"Passing paths like `store.book[0]`, `.foo`, or an empty/whitespace-only string to JsonPath.compile/read; paths that accidentally lost their leading `$` during string building or URL decoding.","commonSituations":"Building paths dynamically and forgetting the `$.` prefix; stripping `$` to 'normalize' then re-reading; receiving user-supplied path strings without validation.","solutions":["Prefix the path with `$` (absolute) or `@` (relative inside filters), e.g. `store.book` → `$.store.book`.","Trim surrounding whitespace before compiling (leading whitespace is skipped, but content must start with $ or @).","Validate user-supplied paths with a regex like `^[$@]` before passing to JsonPath."],"exampleFix":"// before\nJsonPath.read(json, \"store.book[0].title\");\n// after\nJsonPath.read(json, \"$.store.book[0].title\");","handlingStrategy":"validation","validationCode":"boolean isWellFormedPathStart(String p) {\n    String t = p == null ? \"\" : p.trim();\n    return t.startsWith(\"$\") || t.startsWith(\"@\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.compile(path);\n} catch (InvalidPathException e) {\n    throw new IllegalArgumentException(\"Path must start with $ or @: \" + path, e);\n}","preventionTips":["Always build absolute paths with a leading `$`.","Use `@` only inside filter predicates.","Validate user-supplied paths before compiling."],"tags":["json-path","invalid-path","syntax","compiler"],"backgroundTag":"invalid-path-syntax","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}