Share via

How do I get a property which is an object, converted to a bool?

Falanga, Rod, DOH 940 Reputation points
2026-02-26T16:54:43.5633333+00:00

I've got a C# class in a Minimal API, which gets returned. The class looks like this:

    public class APIResponse
    {
        public APIResponse()
        {
            ErrorMessages = new List<string>();
            Result = new object();
        }

        public bool IsSuccess { get; set; }
        public object Result { get; set; }
        public HttpStatusCode StatusCode { get; set; }
        public List<string> ErrorMessages { get; set; }

I'm using the Result as an object, to return different data types depending upon what the query returns. It's worked fine for integers, etc. But when I try to assign a bool to it, then in causes all sorts of problems in the C# Blazor app. It complains that an object cannot be converted to a ValueKind.

Huh??? I'm assigning a bool to Result. Where is that becoming a ValueKind?

I've spent days trying to figure this out, using Claude, GPT-5, etc. All give me solutions that when I try them still result in an "object cannot be converted to ValueKind"?! Then why in heck are those AIs giving me solutions that only result in the same error???

More important, how do I assign a bool to Result in the Minimal API, and get a bool out in the Blazor app that calls that end point?

Developer technologies | ASP.NET | ASP.NET API
0 comments No comments

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 16,115 Reputation points Microsoft External Staff Moderator
    2026-02-27T04:22:05.0566667+00:00

    Hi @Falanga, Rod, DOH ,

    Thanks for reaching out.

    What’s actually happening isn’t that your bool is magically turning into a ValueKind. The issue comes from using object as the type of Result.

    When your Minimal API (in ASP.NET Core) returns the response, it gets serialized to JSON using System.Text.Json. Because Result is declared as object, the serializer doesn’t preserve the concrete type information.

    On the Blazor side (Blazor), when that JSON is deserialized, Result becomes a JsonElement instead of a bool. Inside JsonElement, there’s a ValueKind property that describes what kind of JSON value it is (True, False, Number, etc.). That’s why you’re seeing errors mentioning ValueKind, you’re not actually holding a bool anymore, you’re holding a JSON wrapper.

    Instead of using object, switch to a strongly typed generic response:

    public class APIResponse<T>
    {
        public bool IsSuccess { get; set; }
        public T Result { get; set; }
        public HttpStatusCode StatusCode { get; set; }
        public List<string> ErrorMessages { get; set; } = new();
    }
    

    Then return:

    return new APIResponse<bool>
    {
        IsSuccess = true,
        Result = true,
        StatusCode = HttpStatusCode.OK
    };
    

    And in Blazor:

    var response = await httpClient.GetFromJsonAsync<APIResponse<bool>>("your-endpoint");
    bool value = response.Result;
    

    Now the type stays consistent end-to-end. No JsonElement, no ValueKind, no casting gymnastics.

    Using object in API DTOs tends to cause exactly this kind of issue because JSON serialization loses the concrete type. Generics keep everything strongly typed and predictable.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.

Answer accepted by question author
  1. Bruce (SqlWork.com) 83,666 Reputation points
    2026-02-26T17:24:03.6966667+00:00

    Value types like int, float, double, bool, etc are not objects. They must be boxed to store as an object type rather than value type. To convert a boxed bool to a bool requires a cast:

    // 1. A boolean value type
    bool originalBool = true;
    
    // 2. Boxing: The bool is implicitly converted to an object
    object boxedObject = originalBool; 
    
    // 3. Unboxing: The object is explicitly cast back to a bool
    bool unboxedBool = (bool)boxedObject; 
    
    

    you don’t show your code so it’s hard to know the error.

    also it would better to make your ApiResponse strongly typed:

        public class APIResponse<T>
        {
            public APIResponse()
            {
                ErrorMessages = new List<string>();
                Result = default(T);
            }
    
            public bool IsSuccess { get; set; }
            public T Result { get; set; }
            public HttpStatusCode StatusCode { get; set; }
            public List<string> ErrorMessages { get; set; }
        }
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. SurferOnWww 5,831 Reputation points
    2026-02-27T01:24:27.0166667+00:00

    But when I try to assign a bool to it, then in causes all sorts of problems in the C# Blazor app. It complains that an object cannot be converted to a ValueKind.

    2026/3/1 Additional info: As mentioned in my answer below Minimal API returns a correct JSON string in which the Result is serialized as expected such like "result":false. Therefore, the server side (i.e., Minimal API) should be OK and your issue should be in the client side. As far as I have tested the client side (Blazor WASM Standalone app), however, the result is also OK as described in my comment below. Please tell me how I can reproduce your issue.

    Minimal API returns a JSON string as expected as shown below. Are you talking about the Blazor app.? If so please provide the information to reproduce your issue.

    APIResponse class (your code as it is)

    using System.Net;
    
    namespace MinimalAPI
    {
        public class APIResponse
        {
            public APIResponse()
            {
                ErrorMessages = new List<string>();
                Result = new object();
            }
    
            public bool IsSuccess { get; set; }
            public object Result { get; set; }
            public HttpStatusCode StatusCode { get; set; }
            public List<string> ErrorMessages { get; set; }
        }
    }
    

    Minimal API

    app.MapGet("/apiresponse", () => 
    {
        var response = new APIResponse
        {
            IsSuccess = true,
            Result = false,
            StatusCode = System.Net.HttpStatusCode.OK,
            ErrorMessages = new List<string>() { "Error", "Message" }
        };
    
        return response;                
    });
    

    Result:

    enter image description here


  2. AgaveJoe 31,186 Reputation points
    2026-02-26T19:16:01.29+00:00

    I'm guessing the error is happening at runtime because of how System.Text.Json handles the object type during deserialization.

    When you define a property as object, the deserializer doesn't know if the incoming JSON is a string, int, or bool, so it wraps it in a JsonElement. When you try to use it in Blazor, you aren't holding a boxed bool, you’re holding a JsonElement struct.

    You need to check the ValueKind of that element before extracting the value. I wouldn't do it this way but here is some code to convey the concept.

    if (apiResponse.Result is JsonElement element)
    {
        if (element.ValueKind == JsonValueKind.True || element.ValueKind == JsonValueKind.False)
        {
            bool myBool = element.GetBoolean();
            // Use your bool here
        }
    }
    

    As Bruce mentioned, using a generic class like APIResponse<T> is the 'gold standard' here. If you know you are expecting a bool, using APIResponse<bool> will allow the deserializer to map it directly to a C# boolean, and you won't have to deal with JsonElement at all.

    Reference documentation

    https://dori-uw-1.kuma-moon.com/en-us/dotnet/api/system.text.json.jsonelement.valuekind?view=net-10.0

    https://dori-uw-1.kuma-moon.com/en-us/dotnet/api/system.text.json.jsonvaluekind?view=net-10.0

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.