JamesNK/Newtonsoft.Json · error · JsonException
Unknown escape character: \
Error message
Unknown escape character: \
What it means
Thrown by ReadQuotedString when a backslash escape inside a single-quoted JSONPath string literal is followed by a character that is not one of the recognized escape codes (b, t, n, f, r, backslash, double-quote, single-quote, forward-slash). Only that fixed set of escapes is supported inside JSONPath quoted strings.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:688
resolvedChar = '\t';
break;
case 'n':
resolvedChar = '\n';
break;
case 'f':
resolvedChar = '\f';
break;
case 'r':
resolvedChar = '\r';
break;
case '\\':
case '"':
case '\'':
case '/':
resolvedChar = currentChar;
break;
default:
throw new JsonException(@"Unknown escape character: \" + currentChar);
}
sb.Append(resolvedChar);
_currentIndex++;
}
else if (currentChar == '\'')
{
_currentIndex++;
return sb.ToString();
}
else
{
_currentIndex++;
sb.Append(currentChar);
}
}
View on GitHub (pinned to 4f73e74372)
Solutions
- Replace unsupported escapes: use \\uXXXX via JSON string rules outside JSONPath, or escape the raw characters differently.
- For literal backslashes use the supported \\\\ escape (backslash-backslash).
- Sanitize/escape user-supplied text before interpolating it into a JSONPath quoted string.
Example fix
// before
token.SelectTokens("$['C:\\new']");
// after
token.SelectTokens("$['C:\\\\new']"); Defensive patterns
Strategy: validation
Validate before calling
static string EscapeForJsonPath(string value)
{
return value.Replace("\\", "\\\\").Replace("'", "\\'");
} Try / catch
try { token.SelectTokens($"$['{field}']").ToList(); }
catch (JsonException ex) when (ex.Message.StartsWith("Unknown escape character"))
{ /* field contained an unsupported escape; sanitize and retry */ } Prevention
- Sanitize interpolated text: escape backslash and single-quote.
- Avoid \uXXXX inside JSONPath quoted strings; it is not supported here.
When it happens
Trigger: A quoted field name or string value containing an unsupported escape. Example: token.SelectTokens("$['foo\\x']") or a query like "$..[?(@.name == 'a\\q')]" where \\x / \\q are not valid escapes.
Common situations: Forwarding user text containing Windows file paths or regex into a JSONPath without sanitization; assuming JSONPath supports the same escapes as C# or JSON verbatim strings (e.g. \uXXXX is not supported here); constructing paths from URIs that contain %2F style sequences.
Related errors
- Path ended with open query.
- Path ended with an open string.
- Path ended with an open regex.
- Could not read query operator.
- Index {0} outside the bounds of JArray.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/da7f79189619e801.
Report an issue: GitHub.