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

Today in this article, we will see how to fix the issue “The JSON value could not be converted to System.Int32 “

Issue Description

Serialization/Deserialization in .NET Core or ASP.NET Core 3.1 or .NET 5.0 gives below or similar error,

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

InvalidOperationException: Cannot get the value of a token type ‘String’ as a number.

SystemTextJson The JSON value could not be converted to SystemInt32

Resolution

System.Text.Json doesn’t convert such string field directly to Int without using a custom int converter.

I had the issue while using the below sample JSON.

Sample JSON

SystemTextJson The JSON value could not be converted to SystemInt32

System.Text.Json doesn’t deserialize non-string values like Int, Boolean and other primitives into string properties. Any non-string value conversion produces JsonException with the following message,

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

public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int EmployeeId { get; set; }
        public Address Address { get; set; }
    }

    public class Address
    {
        public int ZipCode { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

The below call fails to Deserialize the above JSON to custom type Customer

  
Customer empObject = JsonSerializer.Deserialize<Customer>(jsonOutPut);

This issue is more because the new .NET /ASP.NET Core 3.1 framework has removed the dependency on JSON.NET and uses its own JSON serializer i.e ‘System.Text.Json‘.

There are known limitations in the System.Text.Json serializer which are as per specification and design.

Resolution 1(Recommended):

These limitations can be overcome by using Custom Int32Converter.

           
            var serializeOptions = new JsonSerializerOptions();

            serializeOptions.Converters.Add(new Int32Converter());

            Customer empObject = JsonSerializer.Deserialize<Customer>
                                 (jsonOutPut, serializeOptions);


Int32Converter can be easily created using simple steps as explained below article,

Resolution 2:

Continue using Newtonsoft as is.

I was able to serialize the same using NewtonSoft(JSON.NET) without any issues,

SystemTextJson The JSON value could not be converted to SystemInt32

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!

Reference :



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “System.Text.Json: The JSON value could not be converted to System.Int32

    1. Hi Abdulaziz- JsonOutput is the JSON data received which I have highlighted in the screenshot. System.Text.Json doesn’t convert such string field directly to Int without using custom Int32 converter.

Leave a Reply

Your email address will not be published. Required fields are marked *