JSON Errors / C#

System.Text.Json.JsonException: 'x' is an invalid start of a value

How to Fix: System.Text.Json.JsonException: 'x' is an invalid start of a value

System.Text.Json could not parse the input - the message quotes the offending character and gives the position as LineNumber and BytePositionInLine. '<' means HTML came back instead of JSON; "'" means single quotes; 'x' at position 0 often means plain text.

A related .NET-specific trap: deserialization can also *succeed* and hand you nulls, because System.Text.Json is case-sensitive by default - a payload with "name" leaves a Name property null without any exception.

{"find": "error"}paste your JSON below

Common causes

1. HTML or plain-text response

An error page or gateway message parsed as JSON - check response.IsSuccessStatusCode before deserializing.

2. Case mismatch: no exception, just nulls

JSON "name" does not bind to C# property Name unless you opt in to case-insensitive matching or use [JsonPropertyName].

Invalid
// {"name": "Ada"} -> user.Name == null (silently!)
Valid
var opts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

3. Trailing commas or comments in config JSON

Strict by default. appsettings-style JSON with comments needs JsonCommentHandling.Skip and AllowTrailingCommas.

How to fix it

Deserialize defensively
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var order = JsonSerializer.Deserialize<Order>(json, options)
    ?? throw new InvalidOperationException("payload was JSON null");
Accept comments and trailing commas (config files)
var options = new JsonSerializerOptions
{
    ReadCommentHandling = JsonCommentHandling.Skip,
    AllowTrailingCommas = true,
};

Frequently asked questions

Why does Deserialize<T> return null without throwing?

Only when the input is the literal JSON "null" - any other invalid input throws JsonException. The nullable return type exists for that one case; the null-coalescing throw pattern above handles it explicitly.

How do I see exactly where parsing failed?

Catch JsonException and read ex.LineNumber, ex.BytePositionInLine, and ex.Path - Path is the JSON path ($.items[2].price) to the failing token, which pins the problem immediately in large payloads.

System.Text.Json or Newtonsoft.Json for new code?

System.Text.Json - it ships with .NET, is faster, and gets the platform investment. Newtonsoft remains fine in existing code and still wins for a few exotic features (deep LINQ-to-JSON mutation). Their defaults differ (case sensitivity, comments), which is exactly what bites during migrations.

Last updated:

Related JSON errors

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →