Code Beautifier
The JSON value could not be converted to System.Int32

Fix "The JSON value could not be converted to System.Int32"

System.Text.Json found a string, a decimal, or a null where the property is int. Why the generator uses long and long?, and how to fix each case.

Input that triggers it

{"id": "42", "total": 18.25, "rank": null}
Open JSON to C# on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Serializer:
Type:
json
C#csharp

What the error means

System.Text.Json is strict about numeric conversion. JsonException: The JSON value could not be converted to System.Int32. Path: $.id | LineNumber: 0 | BytePositionInLine: 10 means the value at that path could not be read as a 32-bit integer. The Path is the useful part: $.id points straight at the property. The same message appears with System.Int64, System.Boolean, and other primitives.

The example triggers it three ways: "id" is a string, total has a fractional part, and rank is null in a non-nullable property.

Why it happens

  • A quoted number. "42" is a string; System.Text.Json does not read it into int unless told to. Newtonsoft.Json does, which is why migrating from Newtonsoft surfaces this.
  • A fractional value for an integer property. 18.25 cannot be an int; it needs decimal or double.
  • Null for a non-nullable value type. int cannot hold null; int? can.
  • The class was generated from an unrepresentative sample, so the property types reflect the sample rather than the data.

How to fix it

  1. Paste a representative payload into the generator above. It emits long for integers (a safer default than int for IDs and counters), double for any field with a fractional sample, string for quoted values, and long? for a field that is ever null.
  2. In an existing class, change the property type to match the data at the reported path.
  3. For quoted numbers you would rather coerce than retype, enable it on the options: new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString }, or annotate the property with [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)].
  4. For null, make the property nullable (long?) rather than suppressing the error.

The class the example produces:

public class Root
{
    [JsonPropertyName("id")]
    public string Id { get; set; }

    [JsonPropertyName("total")]
    public double Total { get; set; }

    [JsonPropertyName("rank")]
    public long? Rank { get; set; }
}

If it still fails

  • Read the Path in the exception. $.items[3].price means the third element of items, not the top-level object; the fix goes on the element's class.
  • Jackson's equivalent for the quoted-number case is InvalidFormatException: Cannot deserialize value of type \long` from String`; JSON to Java covers the same decision.
  • If the value is an identifier rather than a quantity, string is the honest type — JSON to TypeScript will show it the same way.

Related errors