{"record":{"id":"3772054dd99d887a","repo":"json-path/JsonPath","slug":"failed-to-parse-filter","errorCode":null,"errorMessage":"Failed to parse filter: ","messagePattern":"Failed to parse filter: ","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java","lineNumber":89,"sourceCode":"        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();\n             if (filter.inBounds()) {\n                 throw new InvalidPathException(String.format(\"Expected end of filter expression instead of: %s\",\n                         filter.subSequence(filter.position(), filter.length())));\n             }\n\n             return result;\n        } catch (InvalidPathException e){\n            throw e;\n        } catch (Exception e) {\n            throw new InvalidPathException(\"Failed to parse filter: \" + filter + \", error on position: \" + filter.position() + \", char: \" + filter.currentChar());\n        }\n    }\n\n    private ValueNode readValueNode() {\n        switch (filter.skipBlanks().currentChar()) {\n            case DOC_CONTEXT  : return readPath();\n            case EVAL_CONTEXT : return readPath();\n            case NOT:\n                filter.incrementPosition(1);\n                switch (filter.skipBlanks().currentChar()) {\n                    case DOC_CONTEXT  : return readPath();\n                    case EVAL_CONTEXT : return readPath();\n                    default: throw new InvalidPathException(String.format(\"Unexpected character: %c\", NOT));\n                }\n            default : return readLiteral();\n        }\n    }\n","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java#L71-L107","documentation":"compile() catches any unexpected exception during parsing (IndexOutOfBounds, NumberFormat, etc.) and rethrows it as InvalidPathException with 'Failed to parse filter:' plus the filter text, position, and offending character. This is the catch-all for filter bodies that are structurally parenthesized correctly but contain invalid expressions — bad literals, stray operators, or unbalanced inner brackets.","triggerScenarios":"A filter body that throws during tokenization: e.g. '[?(@.price < )]' (missing operand), '[?(@.a === 1)]' (unsupported operator), unmatched quote \"[?(@.name == 'x)]\", or a regex/inline value the parser cannot handle.","commonSituations":"Typos in operators (===, =>) copied from JavaScript; unbalanced quotes in string literals; nested JSON in comparisons without proper quoting; upgrading JsonPath versions where stricter parsing rejects previously lenient filters.","solutions":["Read the reported position and char in the message to locate the offending token","Fix the operator syntax: JsonPath supports ==, !=, <, <=, >, >=, =~, in, nin, size, empty, matches, and logical &&/||","Balance all quotes in string literals inside the filter; prefer double quotes escaped correctly in Java source","Simplify the filter into smaller Criteria and combine with Criteria.and()/or() to isolate the malformed part","Pin/check library version — newer releases emit more precise errors for the same input"],"exampleFix":"// before\nJsonPath.compile(\"$..book[?(@.name === 'x')]\");\n// after\nJsonPath.compile(\"$..book[?(@.name == 'x')]\");","handlingStrategy":"try-catch","validationCode":"static boolean hasBalancedQuotes(String filter) {\n    int q = 0;\n    for (char c : filter.toCharArray()) { if (c == '\\'') q++; }\n    return q % 2 == 0;\n}","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.compile(path);\n} catch (InvalidPathException e) {\n    if (e.getMessage().startsWith(\"Failed to parse filter\")) {\n        log.error(\"Filter parse failed at reported position: {}\", e.getMessage());\n        throw new IllegalArgumentException(\"Unsupported filter expression: \" + path, e);\n    }\n    throw e;\n}","preventionTips":["Use only JsonPath-supported operators (==, !=, <, <=, >, >=, =~, in, nin, size, empty, matches) — no JavaScript '==='","Check quote balance in string literals inside filters","Build complex predicates with Criteria.and()/or() to avoid hand-assembled expression bugs","Compile all query strings in a test suite; re-run when bumping the JsonPath version"],"tags":["json-path","filter-syntax","expression-parse"],"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"}