{"record":{"id":"4f2bda8f8ea86907","repo":"json-path/JsonPath","slug":"filter-must-start-with-and-end-with","errorCode":null,"errorMessage":"Filter must start with '[' and end with ']'. ","messagePattern":"Filter must start with '\\[' and end with '\\]'\\. ","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java","lineNumber":61,"sourceCode":"    private static final char TRUE = 't';\n    private static final char FALSE = 'f';\n    private static final char NULL = 'n';\n    private static final char NOT = '!';\n    private static final char PATTERN = '/';\n    private static final char IGNORE_CASE = 'i';\n\n    private CharacterIndex filter;\n\n    public static Filter compile(String filterString) {\n        FilterCompiler compiler = new FilterCompiler(filterString);\n        return new CompiledFilter(compiler.compile());\n    }\n\n    private FilterCompiler(String filterString) {\n        filter = new CharacterIndex(filterString);\n        filter.trim();\n        if (!filter.currentCharIs('[') || !filter.lastCharIs(']')) {\n            throw new InvalidPathException(\"Filter must start with '[' and end with ']'. \" + filterString);\n        }\n        filter.incrementPosition(1);\n        filter.decrementEndPosition(1);\n        filter.trim();\n        if (!filter.currentCharIs('?')) {\n            throw new InvalidPathException(\"Filter must start with '[?' and end with ']'. \" + filterString);\n        }\n        filter.incrementPosition(1);\n        filter.trim();\n        if (!filter.currentCharIs('(') || !filter.lastCharIs(')')) {\n            throw new InvalidPathException(\"Filter must start with '[?(' and end with ')]'. \" + filterString);\n        }\n    }\n\n    public Predicate compile() {\n        try {\n             final ExpressionNode result = readLogicalOR();\n             filter.skipBlanks();","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java#L43-L79","documentation":"FilterCompiler validates that a filter expression string starts with '[' and ends with ']' before parsing it. If the trimmed filter string does not have these delimiters, an InvalidPathException with this message is thrown. This is Jayway JsonPath's way of rejecting malformed filter syntax like '?(x)' instead of '[?(x)]'.","triggerScenarios":"Calling JsonPath.compile() or Filter/Path building APIs with a filter string whose first non-blank character is not '[' or whose last non-blank character is not ']', e.g. JsonPath.compile(\"$..book[?(@.price < 10)]\") mistyped as \"$..book(@.price < 10)\" or with a stray trailing character.","commonSituations":"Hand-written JSONPath strings with missing or unbalanced brackets; filters interpolated from templates or config; trailing whitespace/comments left after the closing bracket in hand-edited query strings.","solutions":["Wrap the filter expression in square brackets: the substring passed as a filter must literally begin with '[' and end with ']'","Check for trailing characters after ']' (spaces are trimmed, but any other character breaks the check)","If composing dynamically, build the path as \"$..book[?(\" + predicate + \")]\" so brackets are guaranteed","Verify with a JSONPath evaluator (e.g. https://jsonpath.com) that the query is syntactically valid before embedding it in code"],"exampleFix":"// before\nJsonPath.compile(\"$..book?(@.price < 10)\");\n// after\nJsonPath.compile(\"$..book[?(@.price < 10)]\");","handlingStrategy":"validation","validationCode":"// alternate concise guard\nObjects.requireNonNull(filter);\nif (!filter.trim().startsWith(\"[\") || !filter.trim().endsWith(\"]\"))\n    throw new IllegalArgumentException(\"Filter must be bracketed\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate before compiling; fail fast with your own clearer message"],"tags":["json-path","filter-syntax","invalid-path"],"backgroundTag":"invalid-argument-format","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}