JamesNK/Newtonsoft.Json · error · JsonException

Could not read query operator.

Error message

Could not read query operator.

What it means

Thrown by ParseOperator after none of the supported comparison operators (===, ==, =~, !==, !=, <>, <=, <, >=, >) matched the characters at the current position. The token where an operator was expected is not a recognized JSONPath query operator.

Source

Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:820

            }
            if (Match("<="))
            {
                return QueryOperator.LessThanOrEquals;
            }
            if (Match("<"))
            {
                return QueryOperator.LessThan;
            }
            if (Match(">="))
            {
                return QueryOperator.GreaterThanOrEquals;
            }
            if (Match(">"))
            {
                return QueryOperator.GreaterThan;
            }

            throw new JsonException("Could not read query operator.");
        }

        private PathFilter ParseQuotedField(char indexerCloseChar, bool scan)
        {
            List<string>? fields = null;

            while (_currentIndex < _expression.Length)
            {
                string field = ReadQuotedString();

                EatWhitespace();
                EnsureLength("Path ended with open indexer.");

                if (_expression[_currentIndex] == indexerCloseChar)
                {
                    if (fields != null)
                    {
                        fields.Add(field);

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Use one of the supported operators: === == =~ !== != <> <= < >= >.
  2. For equality use '==' (or '===' for strict). For regex use '=~'. For not-equal use '!=' or '<>'.
  3. Consult the JsonSelectSettings/JSONPath query grammar reference for the exact operator set.

Example fix

// before
token.SelectTokens("$..[?(@.a ~= 5)]");
// after
token.SelectTokens("$..[?(@.a == 5)]");
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> ValidOps = new()
{ "===","==","=~","!==","!=","<>","<=","<",">=",">" };
static bool UsesValidOperator(string path)
{
    foreach (var op in ValidOps) if (path.Contains(op)) return true;
    return !path.Contains("[?(");
}

Try / catch

try { token.SelectTokens(path).ToList(); }
catch (JsonException ex) when (ex.Message == "Could not read query operator.")
{ /* unsupported operator used; show the valid set to the user */ }

Prevention

When it happens

Trigger: A query using an unsupported or misspelled operator. Example: token.SelectTokens("$..[?(@.a ~= 5)]") (~= is invalid; the regex operator is =~), or "$..[?(@.a => 5)]", or "$..[?(@.a eq 5)]" (SQL-style).

Common situations: Confusing JSONPath query syntax with SQL, LINQ, or JavaScript (e.g. 'eq', 'like', '=>'); transposing characters (=~ vs ~=); copy/paste from a tutorial for a different JSONPath dialect; using assignment '=' where '==' was intended.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/b59efcbe7d401e2e. Report an issue: GitHub.