Fix: JSON Deserialization Error in.NET 8

Published: May 25, 2026 · By Kumar Kunal

The Error

Your API throws when posting JSON:

System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $.userId

Or: Each parameter in the deserialization constructor must bind to an object property

Quick Fix - 2 Minutes

.NET 8's System.Text.Json is stricter. Most errors are from 3 causes:

1. Type mismatch - sending string for int:

// You send this
{ "userId": "123" }

// C# expects this public int UserId { get; set; } // CRASH

Fix: Send number or use JsonNumberHandling.AllowReadingFromString

2. Missing constructor parameters:

// This breaks in.NET 8
public class UserDto 
{
    public UserDto(string name) { Name = name; }
    public string Name { get; }
    public int Age { get; set; } // No way to set Age
}

Fix: Add [JsonConstructor] or add setters to all properties

3. Case sensitivity:

{ "userId": 123 }
public int UserID { get; set; } // Note the capital D - CRASH

Fix: Use [JsonPropertyName("userId")] or set PropertyNameCaseInsensitive = true

Why This Happens in.NET 8

Microsoft made System.Text.Json the default and removed Newtonsoft.Json. The new serializer is:

  1. Case-sensitive by default - userId!= UserId
  2. Strict about constructors - All constructor params must match JSON properties
  3. No string-to-number coercion - "123" won't become int 123

Step-by-Step: Global Fix

Add this to Program.cs to make it behave like old Newtonsoft:

builder.Services.Configure<JsonOptions>(options =>
{
    options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

Common Scenarios

  • Frontend sends strings: JavaScript input.value is always string. Use Number() or the option above
  • Immutable records: public record User(string Name, int Age); works. Custom classes need [JsonConstructor]
  • Enums as strings: "Status": "Active" fails for enum Status. Add JsonStringEnumConverter
  • Dates: "2026-06-04" fails for DateTime. Use ISO 8601: "2026-06-04T00:00:00Z"

Best Practice for.NET 8

1. Use records for DTOs: They auto-generate constructors that JSON understands

public record CreateUserRequest(string Name, int Age);

2. Be explicit with attributes when names differ:

public class UserDto 
{
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }
}

Related Dev Fixes

Found this helpful?

Master C# with our complete course. Real apps, real skills, job-ready in 2 hours.

Share this fix: Twitter LinkedIn

Comments on Fix: JSON Deserialization Error in.NET 8 (0)

No comments yet. Be the first to share your thoughts!