JamesNK/Newtonsoft.Json · error · JsonException
Path ended with an open string.
Error message
Path ended with an open string.
What it means
Thrown by ReadQuotedString when the closing single quote of a JSONPath string literal is never found before the end of the input string. The parser scanned forward consuming characters but ran off the end while still inside the quoted string.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:707
}
sb.Append(resolvedChar);
_currentIndex++;
}
else if (currentChar == '\'')
{
_currentIndex++;
return sb.ToString();
}
else
{
_currentIndex++;
sb.Append(currentChar);
}
}
throw new JsonException("Path ended with an open string.");
}
private string ReadRegexString()
{
int startIndex = _currentIndex;
_currentIndex++;
while (_currentIndex < _expression.Length)
{
char currentChar = _expression[_currentIndex];
// handle escaped / character
if (currentChar == '\\' && _currentIndex + 1 < _expression.Length)
{
_currentIndex += 2;
}
else if (currentChar == '/')
{View on GitHub (pinned to 4f73e74372)
Solutions
- Add the missing closing single quote at the end of the field/value.
- When interpolating values, always wrap with opening and closing quotes together (e.g. string.Format("['{0}']", value)).
- Run a quote-count or a small JSONPath linter before evaluation.
Example fix
// before
token.SelectTokens("$['firstname");
// after
token.SelectTokens("$['firstname']"); Defensive patterns
Strategy: validation
Validate before calling
static bool QuotesBalanced(string path)
{
int q = 0;
foreach (var c in path) if (c == '\'') q++;
return q % 2 == 0;
} Try / catch
try { token.SelectTokens(path).ToList(); }
catch (JsonException ex) when (ex.Message == "Path ended with an open string.")
{ /* missing closing quote */ } Prevention
- Always add opening and closing quotes as a pair when interpolating.
- Count single quotes to detect imbalance.
When it happens
Trigger: A quoted field or query value with no closing quote. Example: token.SelectTokens("$['firstname") or token.SelectTokens("$..[?(@.name == 'bob]") where the value quote is never terminated.
Common situations: Manual path strings missing the trailing quote; programmatic string assembly that drops the closing delimiter; copy/paste from a source that used smart quotes; escaping mistakes that swallow the closing quote.
Related errors
- Path ended with open query.
- Unknown escape character: \
- 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/74b55f046d973079.
Report an issue: GitHub.