1

I want to parse JSON dynamic KeyValuePair response to C# model but getting an error. I am building an application using Blazor webassembly (latest version). I am quite sure that I am doing a silly mistake in JSON deserialization. Can you please correct me.

I am getting below JSON response from API:

{
    "message": "Success",
    "data": {
        "stores": {
            "": "Select Store",
            "21": "Indore store",
            "33": "test POS2",
            "34": "test POS4",
            "37": "test store 11",
            "36": "test store09",
            "59": "test store26"
        },
        "storeId": "21",
        "businessId": "1"
    }
}

And trying to parse response like this:

public async Task GetSetFilters()
{
    SetApiHeaders();
    var response = await Http.GetAsync("/pwa/v1/transaction-filters");
    TransactionFiltersModel content = await response.Content.ReadFromJsonAsync<TransactionFiltersModel>();
}

My TransactionFiltersModel is:

public class TransactionFiltersModel
{
    public string message { get; set; }
    public TransactionFiltersData data { get; set; }
}

public class TransactionFiltersData
{
    public string storeId { get; set; }
    public int businessId { get; set; }

    public List<KeyValuePair<string, string>> stores { get; set; }
}

Getting error:

Unhandled exception rendering component: The JSON value could not be converted to System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.String]]. Path: $.data.stores | LineNumber: 1 | BytePositionInLine: 43. System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.String]]. Path: $.data.stores | LineNumber: 1 | BytePositionInLine: 43. at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue (System.Type propertyType) <0x3070550 + 0x00032> in :0 .....

Kuldeep Gill
  • 71
  • 2
  • 11
  • 2
    Make `stores` be a `Dictionary` not a `List>` and it should work. See [Deserializing JSON when key values are unknown](https://stackoverflow.com/a/24901245/3744182) or [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Deserializing JSON with unknown object names](https://stackoverflow.com/q/38688570/3744182). (In fact I can't really see why you chose `List>` instead of `Dictionary`, was there a reason for that?) – dbc Feb 01 '21 at 17:45
  • 2
    Ah, you are using [tag:system.text.json] internally. But the answer is still the same, see [this answer](https://stackoverflow.com/a/58521511/3744182) to [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/q/1207731/3744182) and also [Query or deserialize json with dynamic keys using System.Text.Json](https://stackoverflow.com/q/60089463/3744182). – dbc Feb 01 '21 at 17:51
  • does `Dictionary` work for you or do you really need `List>` for some reason? – dbc Feb 01 '21 at 18:10
  • 1
    Thanks @dbc, Dictionary works. I see somewhere code snippet about List>. Please post your answer so I can mark this question as answered. – Kuldeep Gill Feb 01 '21 at 19:04

0 Answers0